Поскольку звучит так, как будто вы хотите запустить asyncCallB()
последовательно, чтобы вы могли избежать любых дополнительных вызовов в случае сбоя одного из них, тогда это будет проще всего реализовать с помощью async/await
.
. вам нужно будет пометить содержащую функцию как async
, чтобы вы могли использовать await
. Затем вы можете использовать await
для последовательности асинхронных операций:
async function someFunc(inputId) {
try {
let output = await asyncCallA(inputId);
// inputIdsForChildCalls is the list of inputIds for child async
// calls
let inputIdsForChildCalls = [do something with output]
for (let childId of inputIdsForChildCalls) {
let childResult = await asyncCallB(inputIdsForChildCalls[childId]);
// process child result here
// errors in asyncAllB() will have gone to the catch(e) statement below
}
showSuccessToast("Records created successfully!");
} catch(e) {
// handle error here
// throw an error here if you want the caller to be able to see the error
}
}
Для возможно более высокой производительности вы можете запускать операции asyncCallB()
параллельно, как показано ниже, но все вызовы asyncCallB()
будут выполняться , даже если первый из них имеет ошибку (поскольку все они запускаются параллельно):
async function someFunc() {
try {
let output = await asyncCallA(inputId);
// inputIdsForChildCalls is the list of inputIds for child async
// calls
let inputIdsForChildCalls = [do something with output]
let allResults = await Promise.all(inputIdsForChildCalls.map(childId => {
return asyncCallB(childId);
}));
// process allResults array here
// errors will have gone to the catch(e) statement below
showSuccessToast("Records created successfully!");
} catch(e) {
// handle error here
}
}