Originally I had some code that looked like this...
function attachFilesToSelectedItems(file, item, server) {
try {
return await Promise.all(
files.map(file => {
return items.map(item=> {
const formData = new FormData();
formData.append("attachment", file);
return server.addAttachment(item, formData);
});
})
);
} catch {
return _strings.uploadError;
}
}
But this doesn't seem to work as expected, it doesn't wait for all the server.addAttachment calls to finish.
Changing it around to not use maps and make a new Promise does fix it.
function attachFilesToSelectedItems(file, item, server) {
const promises = [];
files.forEach(file => {
items.forEach(item => {
const formData = new FormData();
formData.append("attachment", file);
promises.push(server.addAttachment(item, formData));
});
});
return Promise.all(promises).catch(() => {
return _strings.uploadError;
});
}
Why does the approach to chain map values and using async/await not work?