[英]Bluebird Promise serial iteration, and resolve to modified array?
我有这个承诺,如果在数据库中找不到它,则会创建一个新的Item
文档,然后将其存储在先前创建的Collection
文档中。
Collection文档是数组中的第一个字符串,数组中的任何后续索引都转换为一个或多个Item文档。
Promise.each “解析为未修改的原始数组” ,因此Promise.each
的最后一个return
呈现对象,但后续的.then
生成原始数组。
这是承诺(缩写为可读性):
globalVar = true;
collectionId = "";
var itemSeries = Promise.each(items, function(element) {
if (globalVar == true) {
return Models.Collection.findOneAsync({
"name": element
})
.then(function(collection) {
// promise chain similar to the following else..
// set the collectionId var to an _id
});
} else {
return Models.Items.findOneAsync({
"name": element
})
.then(function(item) {
if (item == null) {
return Models.Labels.findOneAsync({
"title": element
})
.then(function(label) {
var newItem = new Models.Items({
name: element,
label: label._id
});
return newItem.saveAsync();
}).then(function() {
return Models.Items.findOneAsync({
"name": element
});
}).then(function(item) {
item.collection = collectionId;
return item.saveAsync();
}).then(function() {
return Models.Items.findOneAsync({
"name": element
});
}).then(function(item) {
allItems.push(item);
console.log("allItems: [ ");
console.log(allItems);
return allItems;
});
}
});
}
}).then(function(allItems) {
console.log("allItems: [ ");
console.log(allItems);
return allItems;
});
这是Promise.each中最后一个console.log
:
allItems: [
[ { _id: 54eec5f2b9fb280000286d52,
name: 'one',
label: 54eec5f2b9fb280000286d51,
collection: 54eec5f2b9fb280000286d50,
__v: 0 },
{ _id: 54eec5f2b9fb280000286d54,
name: 'two',
label: 54eec5f2b9fb280000286d53,
collection: 54eec5f2b9fb280000286d50,
__v: 0 } ]
然后在随后的.then(function(allItems) {
这里是最后一个console.log
:
allItems: [
[ 'collectionName', 'one', 'two' ]
另外,变量itemSeries
= Promise.each
稍后在Promise.join中呈现undefined
?
.each
函数不会更改通过链传递的值:
我简化了你的代码,作为输入我假设:
var items = ['one','two'];
对于你的代码:
Promise.each(items, function(element) {
return element+'.';
//return Promise.resolve(element+'.');
})
.then(function(allItems) {
console.dir(allItems);
});
结果仍然是['one','two']
因为这是数组items
已解析值。 内返回的每个值不影响传递给链价值的内容then
。
另一方面.map
函数会产生这种效果:
Promise.map(items, function(element) {
return element+'.';
//return Promise.resolve(element+'.');
})
.then(function(allItems) {
console.dir(allItems);
});
这里return
值将用于创建一个新数组,然后将其传递给then
。 这里的结果是['one.','two.']
。
出现在代码中的两个allItems
是不同的对象。
编辑对于使用映射的连续迭代,我会编写一个这样的辅助函数:
function mapSeries(things, fn) {
var results = [];
return Promise.each(things, function(value, index, length) {
var ret = fn(value, index, length);
results.push(ret);
return ret;
}).thenReturn(results).all();
}
来源: 实施Promise.series
Bluebird现在原生实现了mapSeries
,请参阅http://bluebirdjs.com/docs/api/promise.mapseries.html
看起来Promise.each
仍然在Promise.each
仍然返回原始数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.