我已经将我的代码重组为承诺,并构建了一个由多个回调组成的精彩长扁平承诺链.then().最后我想返回一些复合值,并且需要访问多个中间承诺结果.但是,序列中间的分辨率值不在最后一个回调的范围内,我该如何访问它们?
function getExample() {
return promiseA(…).then(function(resultA) {
// Some processing
return promiseB(…);
}).then(function(resultB) {
// More processing
return // How do I gain access to resultA here?
});
}
Run Code Online (Sandbox Code Playgroud) 假设我有一组承诺正在发出网络请求,其中一个会失败:
// http://does-not-exist will throw a TypeError
var arr = [ fetch('index.html'), fetch('http://does-not-exist') ]
Promise.all(arr)
.then(res => console.log('success', res))
.catch(err => console.log('error', err)) // This is executed
Run Code Online (Sandbox Code Playgroud)
让我们说我想等到所有这些都结束了,不管一个人是否失败了.对于我可以没有的资源,可能存在网络错误,但如果我可以获得,我希望在继续之前.我想优雅地处理网络故障.
既然Promise没有留下任何空间,那么在不使用promises库的情况下,处理此问题的推荐模式是什么?
据我所知,在ES7/ES2016中,将多个await代码放在代码中将类似于.then()使用promise 进行链接,这意味着它们将一个接一个地执行,而不是在并行执行.所以,例如,我们有这个代码:
await someCall();
await anotherCall();
Run Code Online (Sandbox Code Playgroud)
我是否理解正确,anotherCall()只有在someCall()完成后才会被调用?并行调用它们的最优雅方式是什么?
我想在Node中使用它,所以也许有一个async库的解决方案?
编辑:我对这个问题中提供的解决方案不满意:由于异步生成器中的非并行等待承诺而减速,因为它使用生成器而我正在询问更一般的用例.
我有一个Promise数组,我正在使用Promise.all(arrayOfPromises)解析;
我接着继续承诺链.看起来像这样
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler();
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
});
Run Code Online (Sandbox Code Playgroud)
我想添加一个catch语句来处理单个promise,以防它出错.但是当我尝试时,Promise.all返回它找到的第一个错误(忽略其余的),然后我无法从其余的数据中获取数据数组中的promise(没有错误).
我尝试过像......
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler()
.then(function(data) {
return data;
})
.catch(function(err) {
return err
});
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
}); …Run Code Online (Sandbox Code Playgroud) 之间有什么区别:
const [result1, result2] = await Promise.all([task1(), task2()]);
Run Code Online (Sandbox Code Playgroud)
和
const t1 = task1();
const t2 = task2();
const result1 = await t1;
const result2 = await t2;
Run Code Online (Sandbox Code Playgroud)
和
const [t1, t2] = [task1(), task2()];
const [result1, result2] = [await t1, await t2];
Run Code Online (Sandbox Code Playgroud) 我正在使用async/ 并行await触发多个api调用:
async function foo(arr) {
const results = await Promise.all(arr.map(v => {
return doAsyncThing(v)
}))
return results
}
Run Code Online (Sandbox Code Playgroud)
我知道的是,不同于loops,Promise.all 执行并行(即,等待换结果部分是并行地).
但我也知道:
如果其中一个元素被拒绝且Promise.all快速失败,则Promise.all被拒绝:如果有四个promise在超时后解析,并且一个立即拒绝,则Promise.all立即拒绝.
当我读到这个时,如果我Promise.all有5个承诺,并且第一个完成返回a reject(),那么其他4个被有效取消并且它们的承诺resolve()值将丢失.
还有第三种方式吗?执行是否有效并行,但单一故障不会破坏整个群体?
似乎有一些问题将async/await与.reduce()结合起来,如下所示:
const data = await bodies.reduce(async(accum, current, index) => {
const methodName = methods[index]
const method = this[methodName]
if (methodName == 'foo') {
current.cover = await this.store(current.cover, id)
console.log(current)
return {
...accum,
...current
}
}
return {
...accum,
...method(current.data)
}
}, {})
console.log(data)
Run Code Online (Sandbox Code Playgroud)
该data对象被记录之前的this.store完成...
我知道你可以使用Promise.all异步循环,但这适用于.reduce()?
这有什么区别吗:
const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
// do some calculations
}
Run Code Online (Sandbox Code Playgroud)
和这个 ?
for await (const res of items.map(e => somethingAsync(e))) {
// do some calculations
}
Run Code Online (Sandbox Code Playgroud)
我知道在第一个片段中,所有的承诺都被同时触发,但我不确定第二个。for 循环是否等待第一次迭代完成以调用下一个 promise ?还是所有的 Promise 都是同时触发的,循环内部就像是它们的回调?
我正在使用生成器和Bluebird编写代码,我有以下内容:
var async = Promise.coroutine;
function Client(request){
this.request = request;
}
Client.prototype.fetchCommentData = async(function* (user){
var country = yield countryService.countryFor(user.ip);
var data = yield api.getCommentDataFor(user.id);
var notBanned = yield authServer.authenticate(user.id);
if (!notBanned) throw new AuthenticationError(user.id);
return {
country: country,
comments: data,
notBanned: true
};
});
Run Code Online (Sandbox Code Playgroud)
但是,这有点慢,我觉得我的应用程序等待I/O太多而且它不是并行的.如何提高应用程序的性能?
总响应时间为800 countryFor+ 400 getCommentDataFor+ + 600,authenticate因此总共1800ms这是很多.
默认情况下,Promise.All([])函数返回一个基于数字的索引数组,该数组包含每个promise的结果.
var promises = [];
promises.push(myFuncAsync1()); //returns 1
promises.push(myFuncAsync1()); //returns 2
Promise.all(promises).then((results)=>{
//results = [0,1]
}
Run Code Online (Sandbox Code Playgroud)
使用Promise.all()返回命名结果索引的最佳方法是什么?
我尝试使用Map,但它以这种方式返回数组中的结果:
[key1, value1, key2, value2]
更新:
我的问题似乎不清楚,这就是为什么我不喜欢基于有序的索引:
results[42]可以通过下面的jib答案修复)var promises = [];
if(...)
promises.push(...);
else{
[...].forEach(... => {
if(...)
promises.push(...);
else
[...].forEach(... => {
promises.push(...);
});
});
}
Promise.all(promises).then((resultsArr)=>{
/*Here i am basically fucked without clear named results
that dont rely on promises' ordering in the array */
});
Run Code Online (Sandbox Code Playgroud) javascript ×10
promise ×7
async-await ×5
es6-promise ×4
asynchronous ×2
bluebird ×2
node.js ×2
babeljs ×1
ecmascript-6 ×1
for-await ×1
generator ×1
reduce ×1
scope ×1