如何兑现一系列承诺?

时间:2019-01-15 23:04:09

标签: javascript promise es6-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.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.logs仍在记录承诺。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

promise的结果作为参数传递给.then()的回调函数,但是您没有使用它。尝试这样的事情:

Promise.all(customs).then((results) => {
      console.log('then', results);
      // ...
});