James111 James111 - 1 year ago 68
Node.js Question

Node.js async parallel not working how it should be?

I've got multiple

' that I want to run one after the other, and I'm not sure I want to be returning the promises as it gets pretty messy!

So I decided to use the
library and implement the
method. Now I noticed that all my promises weren't running one, after the other, instead they were doing what promises are suppose todo (run + finish whenever).

I noticed that all the console.logs were running before all the promises were finished.

(cb) => {
console.log ("hi")
grabMeData (Args)
.then ( (data) => {

// The promise is done and now I want to goto the next functio
(callback) => {
// The above promise is done, and I'm the callback
.then ( () => {
if (Query.error) {
console.log (Query.error); // Determine error here
return; // Return to client if needed
() => {
// The above promise is done and I'm the callback!

// Originally wanted to be async
if (Query.accAlreadyCreated) {
this.NewUserModel.user_id = Query.user_id;
} else {
console.log ("account not created");
console.log ('xx')
], () =>{
console.log ("finished async parallel")

Any reason why my callbacks are being run
the promises are resolved (.then).

Answer Source

like Bergi said, async.js is redundant when you use promise, your code can be simplified as:

  .catch(e => console.error(e))
  .then(() => Query.checkUserExists())
  .then(() => {
    if (Query.accAlreadyCreated) {
      this.NewUserModel.user_id = Query.user_id
      return this.generateToken()
    console.log ("account not created")   
  .then(() => console.log ('xx')  )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download