1

I have a problem using Node JS when making synchronous calls.. Here's my problem:

I have the following code:

async.doWhilst(function(callback) {
    //some code
    callback();
}, function() {
    //make a database call and based on the results I should 
    //return true to continue looping or false to stop here
}, function(err) {
   //do some things when the loop finishes
})

The problem is when calling the database it is asynchronous call and the loop continues before even returning the proper value.

Thank you alot for your comments, I have solved the problem by move the database call to the loop code like this:

var results = []
async.doWhilst(function(callback) {
    //some code
    sequelize.query('some query').success(function(result) {
        results = result;
        callback();
    });
}, function() {
    //use the results variable that is fetched from the database
    //return true to continue looping or false to stop here
}, function(err) {
   //do some things when the loop finishes
})
4
  • The test function should be synchronous, it would be really, really, really bad to try to introduce async code and some kind of wait loop in it Commented Nov 10, 2014 at 8:33
  • I tried to make it synchronous but I can't figure out how, the problem is the database call depends on a variable changes in the loop code Commented Nov 10, 2014 at 8:39
  • You can't make an async operation behave synchronously. You just can't. It doesn't have the same timing as a synchronous operation and cannot be made to. Commented Nov 10, 2014 at 8:45
  • Thank you a lot @JanAagaardMeier your comment gave me a hint to move the database call to the loop code.. I will right an answer to what I have done. Commented Nov 10, 2014 at 8:46

4 Answers 4

1

You can have Sequelize return once the data has actually returned using this method:

return sequelize
  .query("Database query here")
  .success(function(result) {
      //Do something with result here
      else {
          //Return error
      }
  });

Sign up to request clarification or add additional context in comments.

Comments

0

Hope the following can help:

(function iterate() {
   executeDatabaseCall(..., function(e,r) {
      if (...conditions to continue looping are verified...)
         iterate();
      else
         ...manage the end of loop...
   });
})();

Comments

0

If you really want to use sync call, you can use this library :

https://github.com/luciotato/waitfor

It'll permit you to make sync call using :

var syncDataReceived = wait.forMethod(collection,'insert',{data:toinsert});

Comments

0

I have solved the problem by move the database call to the loop code like this:

var results = []
async.doWhilst(function(callback) {
    //some code
    sequelize.query('some query').success(function(result) {
        results = result;
        callback();
    });
}, function() {
    //use the results variable that is fetched from the database
    //return true to continue looping or false to stop here
}, function(err) {
   //do some things when the loop finishes
})

1 Comment

This is not a synchronous Database call.

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.