Promises with ES6

InstructorJacob Carter

Share this video with your friends

Send Tweet

Promises in JavaScript (es6/es2015). Creating and using promises with ECMAscript 2015 in your JavaScript application.

Carlos
~ 8 years ago

I can't access to the code's lesson. The code tab just says "This bin needs a pro account

Embedding JS Bin under https is only available to bins created by users with a pro account.

If you own this bin, you should upgrade today to enable this feature on all your bins."

Mike
~ 8 years ago

Very succinct! Nicely done.

Camilo Rivera
~ 6 years ago

I'm getting UnhandledPromiseRejectionWarning when I run this simple code:

var d = new Promise((resolve, reject) => {
  if (false) {
    resolve('hello world');
  } else {
    reject('no bueno');
  }
});

d.then((data) => console.log('success : ', data));

d.catch((error) => console.error('error : ', error));

The complete response is:

error :  no bueno
(node:12883) UnhandledPromiseRejectionWarning: no bueno
(node:12883) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:12883) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

I don't see this error showing up on the video.

Seems like d.catch() is being fired. I noticed that if comment out d.then(), the warning message disappears.

I'm calling the script from the terminal like node foobar.js.

What I'm doing wrong?

Tested with node v8.14, v10 and v11 under MacOS High Sierra.

Answer

It turns out that then() returns a new promise that, in this case, gets resolved with the parent promise value, which is rejected. So the new promise rejection is not being catched. To catch it simple chain bothd methods: d.then().catch().

Mark Jones
~ 6 years ago

The audio on this lesson is almost non-existent!