Promise returns early - Map array with promises
Promise returns early - Map array with promises
我们有以下代码生成 ajax post 并下载多个文件。
承诺不必等待其他人完成,应该全部异步完成。
async function downloadAllFiles(fileIds, token) {
var promises = fileIds.map(async fileId => {
return await downloadIndividualFile(fileId, token);
});
Promise.all(promises).then(res => {
return res;
});
}
async function downloadIndividualFile(fileId, token) {
return await downloadFile(fileId, token) // makes call to API post etc.
.then(async result => {
// handle the file download result
// save file
})
.catch(error => {
console.log(error);
});
}
然后从另一个文件调用:
await downloadAllFiles(fileIds, token)
.then(res => {
console.log('result from download all');
console.log(res);
})
.catch(error => {
console.log(error);
})
.finally(() => {
console.log('finally');
});
当前发生的是在 post promise 实际完成之前调用 finally(在 downloadFile api 调用中调用)。
如何更改以上内容,以便在调用 await downloadAllFiles
时仅在所有先前的 post 请求完成时才调用 finally。
您需要 return downloadAllFiles
函数中的承诺。更新:
return Promise.all(promises).then(res => {
return res;
});
对您的代码进行一些重构
function downloadAllFiles(fileIds, token) {
var promises = fileIds.map(fileId => return downloadIndividualFile(fileId, token));
return Promise.all(promises);
}
async function downloadIndividualFile(fileId, token) {
try {
const result = await downloadFile(fileId, token);
// handle the file download result
// save file
} catch (error) {
console.log(error);
}
}
downloadAllFiles(fileIds, token)
.then(res => {
console.log('result from download all');
console.log(res);
})
.catch(error => {
console.log(error);
})
.finally(() => {
console.log('finally');
});
如果您正在使用 .then
,则不需要 await
。
我们有以下代码生成 ajax post 并下载多个文件。
承诺不必等待其他人完成,应该全部异步完成。
async function downloadAllFiles(fileIds, token) {
var promises = fileIds.map(async fileId => {
return await downloadIndividualFile(fileId, token);
});
Promise.all(promises).then(res => {
return res;
});
}
async function downloadIndividualFile(fileId, token) {
return await downloadFile(fileId, token) // makes call to API post etc.
.then(async result => {
// handle the file download result
// save file
})
.catch(error => {
console.log(error);
});
}
然后从另一个文件调用:
await downloadAllFiles(fileIds, token)
.then(res => {
console.log('result from download all');
console.log(res);
})
.catch(error => {
console.log(error);
})
.finally(() => {
console.log('finally');
});
当前发生的是在 post promise 实际完成之前调用 finally(在 downloadFile api 调用中调用)。
如何更改以上内容,以便在调用 await downloadAllFiles
时仅在所有先前的 post 请求完成时才调用 finally。
您需要 return downloadAllFiles
函数中的承诺。更新:
return Promise.all(promises).then(res => {
return res;
});
对您的代码进行一些重构
function downloadAllFiles(fileIds, token) {
var promises = fileIds.map(fileId => return downloadIndividualFile(fileId, token));
return Promise.all(promises);
}
async function downloadIndividualFile(fileId, token) {
try {
const result = await downloadFile(fileId, token);
// handle the file download result
// save file
} catch (error) {
console.log(error);
}
}
downloadAllFiles(fileIds, token)
.then(res => {
console.log('result from download all');
console.log(res);
})
.catch(error => {
console.log(error);
})
.finally(() => {
console.log('finally');
});
如果您正在使用 .then
,则不需要 await
。