以下代码没有给出预期的输出:
function downloadData() {
var timeStamp = new Date();
db.upsert("timeData", function(doc) {
if (!doc.nextRun) {
doc.nextRun = timeStamp.getTime();
}
if (doc.nextRun < timeStamp.getTime()) {
console.log("yes")
timeStamp.setSeconds(timeStamp.getSeconds() + 10);
doc.nextRun = timeStamp.getTime();
api.commerce().prices().all().then(function(items) {
return Promise.all(items.map(function(item) {
// do something with item
return;
}));
}).then(function(){
console.log("data processed")
return doc;
});
} else {
console.log("no")
return doc;
}
}).then(function() {
console.log("Done")
}).catch(console.log.bind(console));
}输出为
yes
Done
data processed但我期待着
yes
data processed
Done我知道这是一个承诺的问题,但我不是很有经验,可以使用一些建议。现在,return doc似乎也触发得太晚了,因此数据没有保存。
发布于 2019-09-13 16:03:25
你忘了还你内心的承诺:
return api.commerce().prices().all().then(function(items) {
// ...https://stackoverflow.com/questions/57918550
复制相似问题