0
我有一個問題,基本上我想調用這個異步瀑布完成後的函數。問題是我打電話更新,但是在更新完成之前它會繼續。我想調用的函數取決於這個數據已經被更新了。NodeJS異步完成任務完成之前
該消息甚至未被髮送。但它最終會在數據庫中更新。
function(arg1, arg2, callback) {
console.log('Updating current_pot with amountInPot');
current_pot.findOneAndUpdate(
{},
{'amountInPot': currentlyInPot},
function(err, current) {
if (err) {
throw err;
}
if (current != null) {
console.log('Updated current_pot amountInPot');
callback(null, 'c');
//This message should be sent before the callback continues!
}
});
}
所有代碼:
async.waterfall([
function(callback) {
//remove from db
console.log('Add to local pot!');
currentlyInPot++;
peopleInPot.push([user.local.picture, message[1], user.local.email]);
callback(null, 'a', 'b');
},
function(arg1, arg2, callback) {
console.log('Updating persons credits');
callback(null, 'c', 'd');
},
function(arg1, arg2, callback) {
console.log('Adding new people to db');
var newPeople = new people_pot();
newPeople.email = user.local.email; //Auto increment
newPeople.picture = user.local.picture;
newPeople.message = message[1];
/** success starts */
newPeople.save(function(err) {
if (err) {
throw err;
}
});
callback(null, 'a', 'b');
},
function(arg1, arg2, callback) {
console.log('Updating current_pot with amountInPot');
current_pot.findOneAndUpdate({}, {
'amountInPot': currentlyInPot
},
function(err, current) {
if (err) {
throw err;
}
if (current != null) {
console.log('Updated current_pot amountInPot');
callback(null, 'c');
//This isn't getting completed before the loadpot is called!
}
});
}
], function(err, result) {
// result is 'e'
//add to db
});
這並沒有提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/11812032) –
你怎麼知道這不是答案,如果你知道正確的答案,那麼你應該提供它? – user3151766
總是試圖解釋答案。單行答案永遠不會對你有好處。嘗試將您的答案與OP的代碼結合起來。你的回答似乎更像是一個評論。 –