Просто убедитесь, что вы await
правильно делаете, и что любая функция, которая имеет ожидание, сама по себе async
, и у вас все должно быть в порядке.Следующий код, например, работает просто отлично.
const products = [1,2,3,4,5,6];
async function updateRecord(data) {
return new Promise((resolve, reject) => {
// using timeout for demonstration purposes
setTimeout(async() => {
console.log("pretending I updated", data.id);
resolve();
}, 1000 * Math.random());
});
};
async function getProductInfo(id) {
return new Promise((resolve, reject) => {
// again using timeout for demonstration purposes
setTimeout(async() => {
console.log("fake-retrieved product info for", id);
resolve(await updateRecord({ id }));
}, 1000 * Math.random());
});
};
Promise.all(products.map(pid => getProductInfo(pid)))
.then(() => console.log("we're done."));
.catch(e => console.error("what?", e));