Thursday 20 April 2017

In nodejs how can the callback be called after the whole function is completed including i/o tasks?

I am saving some emails in mongodb database i need to run the callback only when all the db tasks has been done and the whole function is complete.

function updatedb(i,number,messages,user,callback){

    User.findOne({ username: user.username }, function(err, 
                updateuser) {
        if(err) {
            console.log(err); // handle errors!
        }
        if (!err && updateuser !== null) {
         //some db tasks

               if(i<number){i++;

                    callback(i,number,messages,user,updatedb);}
        }
    });
 }



via Avy

No comments:

Post a Comment