Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带for循环的允诺中的允诺_Javascript_Reactjs_Ecmascript 6_Bluebird - Fatal编程技术网

Javascript 带for循环的允诺中的允诺

Javascript 带for循环的允诺中的允诺,javascript,reactjs,ecmascript-6,bluebird,Javascript,Reactjs,Ecmascript 6,Bluebird,我想调用多个api,第二个api需要来自第一个api的东西,我创建了两个承诺,但现在我被卡住了,我如何执行需要完成的事情并等待它们全部完成 createAccount(this.state.item) //promise .then(resp=>{ this.state.albums.forEach(o=>{ //array of object createAlbum(resp.id, { //promise ...o }) }) }) 我使用

我想调用多个api,第二个api需要来自第一个api的东西,我创建了两个承诺,但现在我被卡住了,我如何执行需要完成的事情并等待它们全部完成

createAccount(this.state.item) //promise
.then(resp=>{

  this.state.albums.forEach(o=>{ //array of object
    createAlbum(resp.id, { //promise
      ...o
    })
  })
})

我使用的是
bluebird
我得到了一个提示,使用的是
promise。所有的
但我不知道forEach是如何工作的,我无法控制有多少相册。

听起来你想创建一个帐户,然后创建一些存储在数组中的相册。这正是
的承诺。所有
都是为了:

createAccount(this.state.item)
.then(resp =>
    Promise.all(
        this.state.albums.map(o => createAlbum(resp.id, o))
    )
);
您需要将
创建的承诺返回给调用者,然后
,或者处理其中的错误(例如,通过
.catch
),就像承诺一样


更多关于
Promise.all
和。

不清楚您想要什么,但是如果您想要处理每个相册,您可以使用
Promise.all
Array.prototype.map
您的代码如下所示:

//if this is part of a function you shoud
// return createAccount ...
createAccount(this.state.item) //promise
.then(
  resp=>//arrow function without {} will return first statement
        //resp=>Promise.all(... is same as resp=>{ return Promise.all(...
    Promise.all(
      this.state.albums.map(
        album=>{
          //not sure why you want to keep calling createAlbum
          //  but only have one id, did you want to do album.id?
          createAlbum(resp.id)
          .then(
            o=>{
              //do something with o, album or response, they are all available
              return album;//you acn for example return album
            }
          )
        }
      )  
    )
)
.then(
  results=>{
    //results is an array of whatever o=> returns
  }
).catch(
  error=>{
    console.warn("something went wrong:",error);
  }
);
如果您有任何问题,请告诉我,您可能需要:

  • 您正在使用的代码
  • 在代码中添加一些
    console.log(“album is:”,JSON.stringify(album,undefined,2)
    语句(album就是一个例子)。这样您就可以自己调试一些代码,并确定您认为拥有的数据对象是否就是您拥有的对象
  • 您遇到的任何错误。在浏览器中按F12键,然后查看“控制台和网络”选项卡。控制台将显示错误,您的控制台。日志和网络将显示包含错误/响应的xhr

  • 使用promise.map而不是promise.all,查找下面的示例以供参考-

    Promise.map(this.state.albums, function(fileName) {
        // Promise.map awaits for returned promises as well.
        return x;
    }).then(function() {
        console.log("Done");
    });
    

    欢迎使用Stack Overflow!请看一看,并通读,特别是您希望最外层承诺的分辨率值是多少?关于
    promise的文档如何?所有
    都不清楚?您尝试使用
    promise.all
    是什么样子的?在@Or中解释得很好B但我不明白:(为什么map不是foreach?我怎样才能得到承诺的回应。全部?@SharonChai:“为什么map不是foreach?”你查过map和foreach的对比了吗?“我怎样才能得到承诺的回应。全部?”我建议学习一些关于承诺的教程。调用
    Promise的结果。上面所有的
    都是上面
    然后
    返回的承诺的分辨率值。使用map这样你就可以这样做了?为什么不使用foreach?@SharonChai foreach不返回任何内容。它更适合于以下副作用:“为每封电子邮件发送圣诞卡”,而不是“为每个url提供一个在该url内容中解析的承诺”,这称为将url映射到响应承诺。