試圖找出如何找到功能與async.eachSeries完全一樣的東西,我需要按順序(不是並行)運行的異步操作列表,但無法找到在本地ES6中執行操作的方式,任何人都可以請指教,好嗎?ES6承諾 - 類似async.each?
p.s.想到發電機/產量,但沒有經驗,所以我沒有意識到它可以幫助我。
編輯1
每個請求,這裏有一個例子:
假定這段代碼:
let model1 = new MongooseModel({prop1: "a", prop2: "b"});
let model2 = new MongooseModel({prop1: "c", prop2: "d"});
let arr = [model1 , model2];
現在,我想在它的一系列運行,而不是平行的,所以使用「異步」NPM很容易:
async.eachSeries(arr, (model, next)=>{
model.save.then(next).catch(next);
}, err=>{
if(err) return reject(error);
resolve();
})
我q問題是:在ES6中,我可以在本地進行嗎?沒有NPM的「異步」包?
編輯2
隨着異步/等待它很容易做到:
let model1 = new MongooseModel({prop1: "a", prop2: "b"});
let model2 = new MongooseModel({prop1: "c", prop2: "d"});
let arr = [model1 , model2];
for(let model of arr){
await model.save();
}
你的意思是,第二個函數取決於第一個函數的結果? – thefourtheye
不一定要依靠它,但必須在先前完成後運行。 –
你應該舉一個例子,說明樣本輸入和預期輸出,以正確解釋問題。 – thefourtheye