samedi 28 mars 2015

Callback delayed with q nodejs

I am using the Q promise library for making database calls as follows:



var promise = Q.nfcall(function(){});
promise.then(removeFromOldList(data))
.then(addToNewList(data))
.done();

function removeFromOldList(data) {
console.log('removeFromOldList');

// remove the card from old list
DB.ListTable.findOne({ _id : data.oldId},
function(err, snippet) {
if(err) console.error("An error occurred");
if (err) console.error(err);
})
.exec(function onFind(err, snippet) {
console.log('removed the card from old list');
});
}

function addToNewList(data) {
console.log('addToNewList');

// add the card to the new list
DB.ListTable.findOne({ _id: data.newId},
function(err, snippet) {
if(err) console.error("An error occurred");
if (err) console.error(err);
})
.exec(function onFind(err, snippet) {
console.log('add the card to the new list');
});
}


But the console log gives me:



removeFromOldList


addToNewList


removed the card from old list add the


card to the new list



How can i make the callbacks be executed before the promise is returned.


Aucun commentaire:

Enregistrer un commentaire