本文介绍了Promise.all在babel ES6实施中解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用babel
转换我的[email protected]
代码,但我信守诺言.
I'm using babel
to transpile my [email protected]
code and I'm stuck with promises.
我需要例如可以在q
和bluebird
或angular.$q
中使用的allSettled
类型功能.
I need allSettled
-type functionality that I could use in q
and bluebird
or angular.$q
for example.
在babel的core-js Promise
上,没有allSettled
方法.
On babel's core-js Promise
, there is no allSettled
method.
当前,我正在使用q.allSettled
作为解决方法:
Currently I'm using q.allSettled
as a workaround:
import { allSettled } from 'q';
babel polyfill中有类似的东西吗?或者,这是我尝试实施的一种很好的算法?
Is there something like that in babel polyfill? Alternatively, which is a good algorithm for me to try to implement?
推荐答案
- 使用执行程序功能创建新的承诺
- 在执行程序的范围内使用计数器/结果数组
- 向每个父对象注册一个then()回调,并将结果保存在数组中
- 当计数器指示所有父项承诺均已完成时,从步骤1解析/拒绝承诺
- create a new promise with an executor function
- use a counter/result array in the scope of the executor
- register a then() callback with each parent promise saving the results in the array
- resolve/reject promise from step 1 when counter indicates that all parent promises are done
这篇关于Promise.all在babel ES6实施中解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!