use*_*931 105 javascript asynchronous node.js promise
我有一个类似于数组的数组,它暴露了异步方法.async方法调用返回数组结构,进而显示更多异步方法.我正在创建另一个JSON对象来存储从此结构获取的值,因此我需要小心跟踪回调中的引用.
我编写了一个强力解决方案,但我想学习一个更惯用或更干净的解决方案.
这是一些部分代码 -
var jsonItems = [];
items.forEach(function(item){
var jsonItem = {};
jsonItem.name = item.name;
item.getThings().then(function(things){
// or Promise.all(allItemGetThingCalls, function(things){
things.forEach(function(thing, index){
jsonItems[index].thingName = thing.name;
if(thing.type === 'file'){
thing.getFile().then(function(file){ //or promise.all?
jsonItems[index].filesize = file.getSize();
Run Code Online (Sandbox Code Playgroud)
Ben*_*aum 333
使用一些简单的规则非常简单:
then都要返回 - 任何你不返回的promise都不会在外面等待..all它们就会等待所有的promise,并且没有任何错误会被沉默.then,你通常可以在中间返回 - then链通常最多1级深.还有一些提示:
.map比for/push -如果你有一个函数映射值,map让你简洁地表达应用动作一招一式综合这些结果的概念.Promise.all不是一个接一个地执行 - 每个都在下一个之前等待.好的,让我们开始吧:
var items = [1, 2, 3, 4, 5];
var fn = function asyncMultiplyBy2(v){ // sample async action
return new Promise(resolve => setTimeout(() => resolve(v * 2), 100));
};
// map over forEach since it returns
var actions = items.map(fn); // run the function over all items
// we now have a promises array and we want to wait for it
var results = Promise.all(actions); // pass array of promises
results.then(data => // or just .then(console.log)
console.log(data) // [2, 4, 6, 8, 10]
);
// we can nest this of course, as I said, `then` chains:
var res2 = Promise.all([1, 2, 3, 4, 5].map(fn)).then(
data => Promise.all(data.map(fn))
).then(function(data){
// the next `then` is executed after the promise has returned from the previous
// `then` fulfilled, in this case it's an aggregate promise because of
// the `.all`
return Promise.all(data.map(fn));
}).then(function(data){
// just for good measure
return Promise.all(data.map(fn));
});
// now to get the results:
res2.then(function(data){
console.log(data); // [16, 32, 48, 64, 80]
});
Run Code Online (Sandbox Code Playgroud)
Ste*_*gin 38
这是一个使用reduce的简单示例.它以串行方式运行,维护插入顺序,并且不需要Bluebird.
/**
*
* @param items An array of items.
* @param fn A function that accepts an item from the array and returns a promise.
* @returns {Promise}
*/
function forEachPromise(items, fn) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item);
});
}, Promise.resolve());
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
var items = ['a', 'b', 'c'];
function logItem(item) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
console.log(item);
resolve();
})
});
}
forEachPromise(items, logItem).then(() => {
console.log('done');
});
Run Code Online (Sandbox Code Playgroud)
我们发现将可选上下文发送到循环中很有用.上下文是可选的,并由所有迭代共享.
function forEachPromise(items, fn, context) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item, context);
});
}, Promise.resolve());
}
Run Code Online (Sandbox Code Playgroud)
你的承诺函数看起来像这样:
function logItem(item, context) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
console.log(item);
context.itemCount++;
resolve();
})
});
}
Run Code Online (Sandbox Code Playgroud)