New promise reject?

10 views
Skip to first unread message

Kevin Burton

unread,
Jan 1, 2016, 2:40:52 PM1/1/16
to Q Continuum (JavaScript)
OK. Something I don't understand about promises. Here is the code:

function A() {
 
return new Promise(function(resolve, reject) {
 setTimeout
(function () {
 reject
(1000);
 
}, 1000);
 
});
}
function B() {
 
return new Promise(function(resolve, reject) {
 setTimeout
(function () {
 reject
(2000);
 
}, 2000);
 
});
}
function C() {
 
return new Promise(function(resolve, reject) {
 setTimeout
(function () {
 reject
(3000);
 
}, 3000);
 
});
}
function D() {
 
return new Promise(function(resolve, reject) {
 setTimeout
(function () {
 reject
(4000);
 
}, 4000);
 
});
}

function ABCD() {
 
var workqueue = [
 A
,
 B
,
 C
,
 D
 
];
 
return Promise.all(workqueue.map(function(work) {
 
return work();
 
})).catch(function(err) {
 
return err;
 
});
}

ABCD
().then(function(result) {
 console
.log(result);
}).catch(function(err) {
 console
.log(err);
});


Each function is called but my understanding is that when any (the first) is rejected then the 'all' promise is in error. What I am seeing from the above is that instead of the result showing up in the catch block the "rejection" shows up in the normal resolve. How can I get it to show up in the catch as an error?

Thank you.

Kevin Burton

unread,
Jan 1, 2016, 2:48:43 PM1/1/16
to Q Continuum (JavaScript)
Sorry I changed it to:

return Promise.all(workqueue.map(function(work) {
     
return work().then(function(result) {
         
return result;
     
}).catch(function(err) {
         
return err;
     
});
})).catch(function(err) {
     
return err;
});

Now it returns an array of rejection values. I would expect only one rejection.

Tom Robinson

unread,
Jan 1, 2016, 7:57:27 PM1/1/16
to Q Continuum (JavaScript)
The ".catch()" calls are catching the rejections, just like a "catch" clause in a normal try/catch would do, i.e. your revised version is equivalent to this:
try { return workqueue.map(function(work) {
    try {
return work();
     
} catch (err) {
       
return err;
     
}
}); } catch (err) {
return err;
}

Remove both calls to "catch" to allow the error to propagate: https://tonicdev.com/tlrobinson/promise-example 

Alternatively you can re-throw the error inside the catch clause.

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Burton

unread,
Jan 2, 2016, 12:46:27 AM1/2/16
to q-con...@googlegroups.com
How do I "rethrow the error " ? Is throw ... Equivalent to reject? Thank you.
You received this message because you are subscribed to a topic in the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/q-continuum/uhpiUUiVeL8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to q-continuum...@googlegroups.com.

Tom Robinson

unread,
Jan 2, 2016, 2:35:27 PM1/2/16
to q-con...@googlegroups.com
Yes, "throw x;" inside a "then" or "catch" handler will cause the resulting promise to be rejected with x, analogous to re-throwing an error in a regular catch clause.

I recommend reading a detailed tutorial on promises, like this: http://www.html5rocks.com/en/tutorials/es6/promises/
Reply all
Reply to author
Forward
0 new messages