javascript - array of promises and Promise.all() -


i have array of promises like:

let promisesarray = [     service1.load('blabla'),     service2.load(), // throws errors ]; 

and want execute them , catch errors this

promise.all(promisesarray)     .then(() => dostuffs())     .catch((err) => handleerror(err)); 

that's working fine want in then() of promise:

baseservice()     .then(() => promise.all([         service1.load('blabla'),         service2.load(), // throw errors     ]))     .catch((err) => handleerror(err)); 

this one's works fine long write array directly in promise.all(), if want use promisearray define earlier like:

baseservice()     .then(() => promise.all(promisesarray))     .catch((err) => handleerror(err)); 

then, catch() run expected have error in console

publish.js:45784 exception: error: uncaught (in promise): ... 

but want use last solution array generated according conditions pushing promises it. (and first example working fine, don't different)

adding catch each of promises while adding them array save problem, i'd find better solution.

i appreciate that.

ps: using angular2 zone.js if changes something

as execute async function (one returns promise) begins running task in background (kindof) promise may resolve or reject @ time moment run

let promisesarray = [     service1.load('blabla'),     service2.load(), // throws errors ]; 

these services going off , loading data, if return before .then() attached, hold onto value , call promisesarray[0].then(x => console.log(x)) function run value

however if 1 of these services throws error , there no .catch function attached yet, hold onto error in order send .catch() function later specified, throw console error - because dont know if there ever catch function attached, , frustrating if promises silently failed , errors disappeared.

if truely want promisesarray execute after baseservice(), idea of making promisesarray array of functions kick off async task , return promise - one. may nicer pass in function returns array of promises, rather passing in array of functions return promises (as stated above)

const getpromises = () => [     service1.load('blabla'),     service2.load(), // throws errors ] 

then execute using

baseservice()     .then(() => promise.all(getpromises()))     .catch((err) => handleerror(err)); 

this start service1.load's after baseservce() has completed, , errors caught occour


Comments