Wednesday, 31 May 2017

Wait On For Loop In Node Promise

I'm trying to run a sequence of promises using the Q library.

The merge notes function creates a new note in the database and I have to run the functions in order due to some unique constraints.

The promises are running in sequence no problem, but I need to push all of the newNotes into an array in workNotes and then resolve that array.

Everything I have tried resolves the promise before the chain is over.

workNotes(notes){
    var _this = this;
    return new Promise(
        function(resolve,reject){
            var chain = Q.when();
            notes.forEach(function(newNote){
                chain = chain.then(function(){
                   return _this.mergeNotes(note);
                 });
             });
        }          
    );
}


mergeNotes(notes){
    return new Promise(
        function(resolve,reject){
            var newNote = doSomething(note);
            resolve(newNote);
         }       
    );
}



via Jeremy Wagner

No comments:

Post a Comment