简体   繁体   English

使用 for 循环的 promise 内的 promise

[英]promise within promise with for loop

I want to call multiple api, the second api need something from the first api, I created two promise but now I'm stuck, how can I execute what needs to be done and wait all of them finish?我想调用多个 api,第二个 api 需要第一个 api 中的一些东西,我创建了两个 promise,但现在我卡住了,我该如何执行需要完成的操作并等待所有这些完成?

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

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

I'm using bluebird I got a hint, using promise.all but I have no clue how that work with forEach, I can't control how many albums are there.我正在使用bluebird我得到了一个提示,使用promise.all但我不知道它是如何与 forEach 一起工作的,我无法控制有多少专辑。

It sounds like you want to create the account, then create some number of albums stored in an array.听起来您想创建帐户,然后创建一些存储在数组中的专辑。 This is indeed exactly what Promise.all would be for:这确实正是Promise.all用途:

createAccount(this.state.item)
.then(resp =>
    Promise.all(
        this.state.albums.map(o => createAlbum(resp.id, o))
    )
);

You'll need to either return the promise created by then to the caller, or handle errors on it (eg, via .catch ), as is always the case with promises.您需要将then创建的承诺返回给调用者,或者处理其上的错误(例如,通过.catch ),就像承诺的情况一样。

More about Promise.all in the bluebird docs and on MDN .有关Promise.all更多信息,请参阅 bluebird 文档MDN

It is not clear what you want but if you want to process each album you can use Promise.all with Array.prototype.map Your code will look something like this:不清楚你想要什么,但如果你想处理每个专辑,你可以使用Promise.allArray.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);
  }
);

Please let me know if you have any questions, will probably need:如果您有任何问题,请告诉我,可能需要:

  1. The code you are using您正在使用的代码
  2. Add some console.log("album is:",JSON.stringify(album,undefined,2) statements in the code (album is an example). So you can debug some of the code yourself and identify if the data objects you think you have are actually the object you have.在代码中添加一些console.log("album is:",JSON.stringify(album,undefined,2)语句(album 就是一个例子)。这样你就可以自己调试一些代码,识别你认为的数据对象你拥有的实际上是你拥有的对象。
  3. Any errors that you have.您遇到的任何错误。 Press F12 in browser and look at console and network tab.在浏览器中按 F12 并查看控制台和网络选项卡。 The console will show errors and your console.logs and network will show your xhr with the errors/response.控制台将显示错误,您的 console.logs 和网络将显示带有错误/响应的 xhr。

Use promise.map instead of promise.all, find example below for reference-使用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");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM