javascript - 如何解决一系列 promise ?

标签 javascript promise es6-promise

我的一系列 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/

相关文章:

Javascript Promise 未定义

javascript - Ajax从函数获取验证,然后将响应传递给另一个ajax?

javascript - promise API

javascript - 在不调用 'then' 的情况下解决 Promise

javascript - 检查我的目标光标是否与我的游戏 "enemy"组件重叠

javascript - 运行 Node 程序时出错。模块 'prompt' 不工作

javascript - 在 Promise 构造函数范围之外解析 Javascript Promise

javascript - ES6 Promise block 页面

javascript - 获取选定的行ID jquery数据表行选择

javascript - Uncaught ReferenceError : $ is not defined error in jQuery