我的一系列 promise 有问题。
const customs = this.state.customElements.map(async el => {
if (el.type === 'paragraph') {
return somePlainObject;
} else {
//uploading an image
await axios.post(url, el.image, {
headers: {
'Content-Type': el.image.type
}
});
return someObject;
}
});
这会返回给我一系列的 promise 。我正在尝试使用 Promise.all() 解决它们,但它似乎不起作用。
Promise.all(customs).then(() => {
console.log('then', customs);
const objectForDatabase = {
title: this.state.inputs.title,
subTitle: this.state.inputs.subTitle,
content: this.state.inputs.content,
mainImg: uploadConfig.data.url,
customs
};
console.log('object for database', objectForDatabase);
});
then
中的两个 console.log 仍在记录 Promise 数组。为什么会发生这种情况?
最佳答案
promise 的结果作为参数传递给 .then()
的回调函数,但您没有使用它。尝试这样的事情:
Promise.all(customs).then((results) => {
console.log('then', results);
// ...
});
关于javascript - 如何解决一系列 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54208104/