Back to Browse

ES6 Tutorial - #11 Promises (then / catch)

11.2K views
Jan 29, 2018
23:45

00:07 What is a Promise? 01:25 Async Operations 02:52 Passed-in Callbacks 03:15 Error Handlers 03:43 Nested Callbacks 04:55 Implementing async functions 10:39 Downsides to using Callbacks 11:34 From Callbacks to Promises 15:23 Refactoring into a Promise Chain 19:15 Benefits of using Promises 22:44 Promise.all() and Promise.race() Time to move on to promises. Promises are all about asyncronous code; think network Ajax requests in the browser or file system operations on the server. Typically, we'd want to hook into those async actions and run a particular logic once each completes. One approach is using callbacks, or effectively, anonymous functions, that are passed around as arguments to the function that will trigger our async operation. This style has accrued popularity with the rise of Node.js, but soon gained notoriety due to what's known as "callback hell". Another alternative is promises, which have been offered through 3rd party libs in accordance with the Promises/A+ spec. With the release of ES6 Harmony, promises are now supported natively in JavaScript. Why bother with promises? After all, they just seem like sugar for good-old callbacks. But they are much more than callbacks. A promise is an object representation of the future result that the async operation will yield once completed. Chained with then() or catch() methods, promises flatten the nested callbacks providing for a structure that resembles synchronous code thereby making it easier to read through and follow. Plus, you get intuitive and centralized error handling mechanism for free (try {...} catch {...} anyone?). Watch the video to learn more! For a yet deeper dive, I strongly encourage you to read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Download

0 formats

No download links available.

ES6 Tutorial - #11 Promises (then / catch) | NatokHD