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.
To clarify the problem, I need to resolve notesList after the chain has completed and every resulting newNote has been pushed to notesList.
workNotes(notes){
var _this = this;
var notesList = [];
return new Promise(
function(resolve,reject){
var chain = Q.when();
notes.forEach(function(note){
chain = chain.then(function(newNote){
_this.notesList.push(newNote);
return _this.mergeNotes(note);
});
});
resolve(notesList)
}
);
}
mergeNotes(notes){
return new Promise(
function(resolve,reject){
doSomething(note)
.then(function(newNote){
resolve(newNote);
})
}
);
}