[英]Sequentially running Q Promises
in a Node.js
App, i want to achieve this: read an array, depend of item type, decide to use an specific function that returns a Q
Promise
object. 在
Node.js
应用程序中,我想实现这一目标:读取数组,取决于项类型,决定使用返回Q
Promise
对象的特定函数。 i want this process runs sequentially. 我希望此过程按顺序运行。
i have this two Promises: 我有这两个承诺:
var q = require('q');
Classes.prototype.fn1 = function (item) {
return q.Promise(function(resolve, reject){
Items.find({item_published: true}).exec(
function (err, events) {
if (err) {
reject('an error happened');
throw err;
}else{
resolve(events);
}
});
});
};
Classes.prototype.fn2 = function (item) {
return q.Promise(function(resolve, reject){
resolve(item);
});
};
And this is main code: 这是主要代码:
self.items = [{},{},{}]; //some items
self.result = [];
self.items.forEach(function(item,index,array){
if(item.type == 1){
self.fn1(item)
.then(function(result){
self.result.push(result);
})
}
if(item.type == 2){
self.fn2(item)
.then(function(result){
self.result.push(result);
})
}
if(index == array.length-1){
callback(self.result);
}
});
But it does not work. 但这行不通。 because that
fn1
has a Async process, it runs after fn2
. 因为该
fn1
具有异步进程,所以它将在fn2
之后运行。 All i want is running these functions sequentially even one of them has Async process. 我想要的是按顺序运行这些功能,即使其中之一具有异步过程。
You can use .reduce
to chain the promises. 您可以使用
.reduce
链接承诺。
var promise = q(); // Create a Resolved promise for chaining.
self.items = [{},{},{}]; //some items
self.result = [];
// We put an resolved promise as init value for chaining
self.items.reduce(function(chain, item) {
// Don't do anything if item type is not match
if (item.type !== 1 && item.type !== 2) {
return chain;
}
var targetFunc = null;
if (item.type === 1) {
targetFunc = self.fn1;
} else if (item.type === 2) {
targetFunc = self.fn2;
}
if (targetFunc === null) {
return chain;
}
// Chain the promise and return the last of the chain.
return chain
.then(function(){
return targetFunc(item);
})
.then(function(result){
// This then will get the result from above
// so we put the result to self.result here
self.result.push(result);
});
}, promise).then(function() {
// When all promises are sequentially resolved,
// call the callback with self.resul.
callback(self.result);
});
jsfiddle , jsfiddle-Q.js ver . jsfiddle , jsfiddle-Q.js ver 。
Had something very similar to below cooking ... but fuyushimoya's just too fast, though we handle reduce's initialization differently 有点类似于下面的烹饪...但是fuyushimoya太快了,尽管我们以不同的方式处理reduce的初始化
var promises = self.items.map( function(item) {
if (item.type == 2) {
return self.fn1(item);
}
else if (item.type == 3) {
return self.fn2(item);
}
});
function handler(p) {
return p.then( function(res) {
self.result.push(res);
});
}
promises
.reduce( function(prev, current) {
if (prev) {
return prev.then( function() { handler(current) } )
}
else {
return handler(current)
}
})
.then(function(result) {
callback(null, result);
})
.catch( // error handler);
Sketching it out. 勾画出来。 But something like this might work.
但是这样的事情可能会起作用。
UPD: the trick was to chain promises as it was mentioned in comments, there is updated version that I used for code snippet here : UPD:诀窍是按照注释中提到的那样实现诺言,这里有我用于代码段的更新版本:
var items = [{type:1},{type:2},{type:1}];
var result = [];
var rq = Q();
var ql = rq;
items.forEach(function (it, ix) {
ql = ql.then(function(){
var dp = "fn" + it.type;
return ps[dp]();
})
.then(function(d) {
result.push(d);
});
});
ql.then(function() {
callback(result);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.