From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Building promises

Building promises - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Building promises

- [Instructor] Promises help us deal with asynchronous behavior in JavaScript. When something is asynchronous, it just means that some sort of weighting is going on. There's a delay between when we ask for something and when we receive it. Kind of the prime example of this is fetching data. I ask for some data from an external API, I wait for a couple seconds and then it comes back to me. So to create our first promise, we will create a delay function. So the delay function here is going to take in a certain number of seconds and then it's going to return a new Promise. New Promise will take in a callback function. So resolve just means if everything goes as expected with the promise, it will resolve and then we can use this setTimeout to wait for a certain number of seconds. The setTimeout function takes in resolve, which stands in for the callback function to fire after the timeout. It also is going to take in a…

Contents