从父承诺中解决一系列承诺 [英] resolving an array of promises from within a parent promise

查看:28
本文介绍了从父承诺中解决一系列承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试嵌套 promise.我正在使用

Promise 只是返回值,您将回调附加到其上,而不是将回调传递给函数.除非您全部返回,否则回调无法链接或捕获所有错误.

此外,当您有另一个承诺时,从所有 .then 返回.这会使事情变得扁平.

This is my first shot at nested promises. I'm using the bluebird library but I think the idea is the same for all of the promise libraries.

At a high level, this is what I'm trying to do:

myService.getSomeData(url)
 .then((data) => {
   myOtherService.getMoreData(data.uniqueId)
   .then((thisDataIsAnArray) => {
      //loop over the data above and do something
   });
 });

getMoreData() is supposed to make X service calls and store the results in an array X elements long. This is where I start getting lost, in that I'm not sure how to craft this method and what I should be returning from it. I've taken a few stabs at bluebird's Promise.all and Promise.map but am floundering and thought I'd solicit suggestions.

解决方案

Promises are just return values you attach callbacks to, instead of passing callbacks into functions. Unless you return all of them, there's no way for the callbacks to chain, or catch all their errors.

Also, return from all the .then's the instant you have another promise. This flattens things.

这篇关于从父承诺中解决一系列承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆