stevekane / promise-it-wont-hurt

A Workshopper module that teaches you to use promises in javascript
737 stars 219 forks source link

#10 An important rule | #bonus part #140

Open mcompote opened 6 years ago

mcompote commented 6 years ago

Greetings! I've stumbled upon some thing I can't comprehend. It is said at the end of lesson's description:

If you are not returning a value from your promise to a caller, then attach a done handler to guard against uncaught exceptions.

And in #bonus sections:

Try swapping your rejection handler from console.log to alwaysThrows. Your program will now throw an exception in the global context! Ahh! Try to fix this using the approach described above.

My code:

function alwaysThrows() { 
    throw new Error("OH NOES");
 }

 function iterate(integer) {  
     console.log(integer);
     return integer+1;
 }

 Promise.resolve( iterate(1) )
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => alwaysThrows() )
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    .then( v => Promise.resolve( iterate(v) ))
    // .catch( e => console.warn(e) )
    .catch( e => alwaysThrows() )
    .done( console.log )

The problem with .done() method of Promise interface. Node refuses to run this code. I looked through MDN articles,etc. but found nothing like .done(). It seems like this method doesn't exist. Btw, this method exists in Q library... Could someone please explain what's wrong with my code. Sorry if my question bothers anyone, im just learning. Thanks in advance