Çözdüğüm bir dizi Sözüm var Promise.all(arrayOfPromises);
Söz zincirine devam etmeye devam ediyorum. Böyle bir şey görünüyor
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();
});
Hata durumunda bireysel bir söz işlemek için bir catch deyimi eklemek istiyorum, ama denediğimde, Promise.all
bulduğu ilk hatayı döndürür (geri kalanını göz ardı eder) ve sonra vaatlerin geri kalanından veri alamıyorum dizi (bu hata vermedi).
Gibi bir şey yapmayı denedim ..
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();
});
Ancak bu çözülmez.
Teşekkürler!
-
Düzenle:
Aşağıdaki cevapların söyledikleri tamamen doğruydu, kod başka nedenlerden dolayı kırılıyordu. Kimsenin ilgilenmesi durumunda, bu benim çözüm buldum ...
Node Express Sunucu Zinciri
serverSidePromiseChain
.then(function(AppRouter) {
var arrayOfPromises = state.routes.map(function(route) {
return route.async();
});
Promise.all(arrayOfPromises)
.catch(function(err) {
// log that I have an error, return the entire array;
console.log('A promise failed to resolve', err);
return arrayOfPromises;
})
.then(function(arrayOfPromises) {
// full array of resolved promises;
})
};
API Çağrısı (route.async çağrısı)
return async()
.then(function(result) {
// dispatch a success
return result;
})
.catch(function(err) {
// dispatch a failure and throw error
throw err;
});
Koymak .catch
için Promise.all
önce .then
orijinal vaatlerinden hataları yakalamak, ama sonra bir sonraki için tüm diziyi dönen amacına hizmet etmiş görünüyor.then
Teşekkürler!
.then(function(data) { return data; })
tamamen atlanabilir
then
veya kod catch
işleyicilerindeki tüm kodu göstermemeniz ve içeride bir hata oluşmasıdır. Bu arada, bu düğüm mü?