ES6诺言 - 像async.each? [英] ES6 Promises - something like async.each?

查看:174
本文介绍了ES6诺言 - 像async.each?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚,如何发现功能酷似async.eachSeries,我需要序列(不平行)运行异步操作的列表,但不能找到一种方法,做到在本地ES6的东西,可以有人建议好吗?

Trying to figure-out how to find something that functional exactly like async.eachSeries, i need a list of async actions run in sequence (not in parallel) but can't find a way to do it in native ES6, can anyone advise please?

P.S。想过发电机/产量,但所以我没有意识到究竟是如何能帮助我没有经验呢。

p.s. thought about generators/yield but don't have experience yet so i'm not realized how exactly it can help me.

**编辑:每个请求的,这里有一个例子:

** per request, here is an example:

假设此code:

let model1 = new MongooseModel({prop1: "a", prop2: "b"});
let model2 = new MongooseModel({prop1: "c", prop2: "d"});

let arr = [model1 , model2];

现在,我想通过它seriesly运行,而不是平行的,所以用了异步NPM很容易:

Now, i want to run over it in seriesly, not parallel, so with the "async" NPM it's easy:

async.eachSeries(arr, (model, next)=>{
    model.save.then(next).catch(next);
}, err=>{
    if(err) return reject(error);
    resolve();
})

我的问题是:与ES6,我能做到这一点本土?没有故宫异步包?

My question is: with ES6, can i do it native? without the NPM 'async' package?

推荐答案

对于那些谁喜欢简短的回答:

For those who like short answers:

[func1, func2].reduce((p, f) => p.then(f), Promise.resolve());

这篇关于ES6诺言 - 像async.each?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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