0
votes

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 (i.e not execute the code further unless the callback is fired.) How can i achieve this?

1
Is it possible to have further execution of code happen from the callback itself?Damon Kaswell
You can use promises;)Alex Nikulin
Trying to block the current thread until a condition is met goes against the core design of nodejs. Even if you manage to do this, it would be unadvisable and had the potential to introduce done sis issues.Don
You can't. Structure your code to work asynchronously.Felix Kling
thanks for the comments guys. I was able to do this as advised by GoodDamon, but i will still have to think over the alternate ways to make this work asyncronously. thanks for all the advise.Rookie

1 Answers

0
votes

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);
  }
]);