简体   繁体   中英

node.js wait for callback to finish?

I have the following code:

            exported.removeWatcher(issueId, user, function(err, result) {
                if (result.statusCode != 204) { // Build a list of failed issues

                } else if (result.statusCode == 204) {

                }
                console.log();
            });

I know that using callbacks is one of the strengths of node.js, but for my use-case, I want to wait until i recieve a response from the above callback (ie not execute the code further unless the callback is fired.) How can i achieve this?

You can use async.js . Just install it with npm and "require" it in your code.

For example:

async.series([ 
  function(callback) {
    myWaitForThisFunction(callback);
  },
  function(callback) {
    myThenDoThisFunction(callback);
  }
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM