Нужно дождаться выполнения всех
fetch().then().then()
, собрав их в массиве.
const promises /* !!! */ = json.dialogs.map((v) => {
let dialogId = v.dialogId;
return /* !!! */ fetch(`${this.WebApiHost}/api/qwe/dialogs/${dialogId}/${this.My_Token}`)
// ....
});
await Promise.allSettled(promises);
А лучше переписать на
async
/
await
и не использовать асинхронные функции в
map
/
forEach
:
try {
const resp = await fetch("");
const json = await resp.json();
if (!json || json.error === true) {
console.log("error");
}
async function handleDialog(dialog) {
try {
const resp = await fetch("");
const json = await resp.json();
// ...
} catch (e) {
// ...
}
}
const concurrently = false;
if (concurrently) {
// [Way 1] Run one by one ---------
for (const dialog of []) {
await handleDialog(dialog);
}
} else {
// [Way 2] Run all concurrently ---
const promises = [];
for (const dialog of []) {
promises.push(handleDialog(dialog));
}
await Promise.allSettled(promises);
}
// The best way: concurrently with limitation
// @see https://qna.habr.com/q/1105356#answer_2098042
// ...
} catch (e) {
// ...
}