rss
0

JavaScript Aync Await or still Bluebird?

  • Sumo

WOW finally we can use async await in JavaScript. A long-long waited feature! But wait! Does it have all the nice things we used to have in Bluebird Promise chain? Well let’s have a quick look at an error handling.

I have designed a function theRejector that is an async function and if you pass an argument “2” it will reject the promise.

So I wanted to test if async [] is equivalent of Promise.all([]). But looks like the answer is NO!

In case of Promise.all all rejections are handled by final catch. But in case of async [] => NO.

Try yourself and propose a solution.

Test is run on Node v7.9.0 version.

 

const Promise = require('bluebird')

const theRejector = async (n) => {
  console.log('start f ' + n)
  if (n === 2) {
    await Promise.delay(1000 * 1)
    return Promise.reject('sik')
  } else {
    await Promise.delay(1000 * 3)
    console.log('end f ' + n)
  }
}

const testRections = async () => {
  // this throws unhandled rejection
  await [theRejector(1), theRejector(2)]

  // this throws rejection that is handled by catch!
  await Promise.all([theRejector(1), theRejector(2)])
}

testRections().catch(console.error)

 

About the Author

Edgar Marukyan is an expert of website development and performance optimization, who maintains and consults clients with high load traffic websites.. If you need an assistance, feel free to drop me a line.         Website - Twitter - Facebook

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.