0

I've got multiple promise' 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 async library and implement the parallel 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.

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

                // The promise is done and now I want to goto the next functio
                cb();
              }).catch(()=>console.log('err'));
        },
        (callback) => {
          // The above promise is done, and I'm the callback
          Query.checkUserExists()
          .then ( () => {
            if (Query.error) {
              console.log (Query.error); // Determine error here
              return; // Return to client if needed
            }
            callback();
          });
        },
        () => {
          // 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;
            this.generateToken();
          } else {
            console.log ("account not created");
          }
          console.log ('xx')
        }
    ], () =>{
      console.log ("finished async parallel")
    });

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

3
  • 3
    If you're trying to run these functions in order, then async.parallel is not the API you need, you should try async.waterfall Commented May 27, 2016 at 2:01
  • Ahhhhh okay @StephenCrosby Thanks Commented May 27, 2016 at 2:02
  • 2
    With promises, you should not use async.js at all. Just chain them using then. Commented May 27, 2016 at 2:31

1 Answer 1

2

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

console.log('hi')
grabMeData(Args)
  .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')  )
Sign up to request clarification or add additional context in comments.

1 Comment

You're 100% right...Not sure why I was thinking to use the async package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.