【发布时间】:2019-07-01 23:25:09
【问题描述】:
我对 JS 比较陌生,无法将承诺概念应用于我拥有的用例,我检查了 this SO 和其他人一样,但无法为我的案例推导出解决方案。 我需要在循环中调用承诺,但只有在循环完成后才应该调用下一个“then”。这在 JS 中是否可行?
function startCooking(ingredients) {
Utility.startConnection()
.then(
function (connectionTime) {
for (let [key, plainVector] of ingredients) {
encryptIngredients(plainVector)
.then(
function (encryptedBinary) {
return Utility.cookDinner();
}
).then(
function (dinner) {
console.log(...);
}
);
}
}
).catch(
...
);
}
function encryptIngredients() {
return new Promise(...);
}
【问题讨论】:
-
这绝对是可能的,但也有点难看。您可以使用 async/await 还是必须支持旧版浏览器?
-
使用 Promise.each bluebirdjs.com/docs/api/promise.each.html
-
我需要使用vanilla JS并且还支持IE11
标签: javascript promise es6-promise