如何将依赖于彼此响应的多个承诺转换为等待并确保触发所有请求
How to turn multiples promises dependent on each other's response into await and make sure all requests fire
我有一个对象数组,其中包含 3 个项目。我正在迭代它,在每个循环中我需要进行 3 API 调用,它们都取决于彼此的响应。所以总共应该完成 9 API 个请求。
const arrayOfObjects = [
{
first: 'thing',
second: 'thing',
third: 'thing'
},
{
fourth: 'thing',
fifth: 'thing',
sixth: 'thing'
},
{
seventh: 'thing',
eight: 'thing',
ninth: 'thing'
},
]
const makeModuleAndBatteryPromises = () => {
arrayOfObjects.map((obj) => {
createModules(obj.first).then((response) => {
createBattery(response, obj.second).then(res => {
assignAssets(res, obj.third).then(res => 'assignment done!');
});
});
});
}
makeModuleAndBatteryPromises();
所以看看上面的代码,似乎我真的只能控制 arrayOfObjects 的第一个循环中的前 3 个 API 调用。因为我有 assignAssets(res).then(res => 'assignment done!');
这将允许我执行一些操作,例如刷新页面或在第一个循环中解决第三个承诺后重定向。
但是我需要在 9 日/最终承诺上做一些手术。这是我尝试让它异步等待的方法。
const makeModuleAndBatteryPromises = async () => {
const promises = arrayOfObjects.map(async obj => {
const firstResponse = await createModules(obj.first);
const secondResponse = await createModules(firstResponse, obj.second);
await assignAssets(secondResponse);
});
await Promise.all(promises)
.then(res => //do some action after all 9 promises resolved)
}
makeModuleAndBatteryPromises()
没有完全达到我的预期,有人可以告诉我我缺少什么吗?
如果我没理解错的话,你想要最终 assignAssets 的解析值吗?
您可能混淆了 async
/await
和 .then
const makeModuleAndBatteryPromises = async() => {
const promises = arrayOfObjects.map(async obj => {
const firstResponse = await createModules(obj.first);
const secondResponse = await createModules(firstResponse, obj.second);
return assignAssets(secondResponse);
});
const res = await Promise.all(promises);
const finalValue = res.at(-1); // or res[res.length-1];
// do things with finalValue
}
makeModuleAndBatteryPromises()
我有一个对象数组,其中包含 3 个项目。我正在迭代它,在每个循环中我需要进行 3 API 调用,它们都取决于彼此的响应。所以总共应该完成 9 API 个请求。
const arrayOfObjects = [
{
first: 'thing',
second: 'thing',
third: 'thing'
},
{
fourth: 'thing',
fifth: 'thing',
sixth: 'thing'
},
{
seventh: 'thing',
eight: 'thing',
ninth: 'thing'
},
]
const makeModuleAndBatteryPromises = () => {
arrayOfObjects.map((obj) => {
createModules(obj.first).then((response) => {
createBattery(response, obj.second).then(res => {
assignAssets(res, obj.third).then(res => 'assignment done!');
});
});
});
}
makeModuleAndBatteryPromises();
所以看看上面的代码,似乎我真的只能控制 arrayOfObjects 的第一个循环中的前 3 个 API 调用。因为我有 assignAssets(res).then(res => 'assignment done!');
这将允许我执行一些操作,例如刷新页面或在第一个循环中解决第三个承诺后重定向。
但是我需要在 9 日/最终承诺上做一些手术。这是我尝试让它异步等待的方法。
const makeModuleAndBatteryPromises = async () => {
const promises = arrayOfObjects.map(async obj => {
const firstResponse = await createModules(obj.first);
const secondResponse = await createModules(firstResponse, obj.second);
await assignAssets(secondResponse);
});
await Promise.all(promises)
.then(res => //do some action after all 9 promises resolved)
}
makeModuleAndBatteryPromises()
没有完全达到我的预期,有人可以告诉我我缺少什么吗?
如果我没理解错的话,你想要最终 assignAssets 的解析值吗?
您可能混淆了 async
/await
和 .then
const makeModuleAndBatteryPromises = async() => {
const promises = arrayOfObjects.map(async obj => {
const firstResponse = await createModules(obj.first);
const secondResponse = await createModules(firstResponse, obj.second);
return assignAssets(secondResponse);
});
const res = await Promise.all(promises);
const finalValue = res.at(-1); // or res[res.length-1];
// do things with finalValue
}
makeModuleAndBatteryPromises()