From the course: Advanced Node.js

Unlock the full course today

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

Resolving promises

Resolving promises - Node.js Tutorial

From the course: Advanced Node.js

Start my 1-month free trial

Resolving promises

- [Instructor] A promise is an object that can be used to represent the eventual completion of an a asynchronous operation. Let's take a look at how we handled asynchronicity within the last lesson using callbacks. So here we have a delay function that creates a delay that waits a certain number of seconds and then invokes a callback. I come over to the terminal and run this. This is our code from the last lesson. We can see the end first tick console log appears first, because that is all of the processing on the first tick. And then we can see our asynchronous callback working. The delay has ended gets logged to the console after a second, so after some time has passed. Now promises are another way that we can deal with asynchronicity within our code. So I'm gonna go ahead and create the exact same function using a promise. This time we're not gonna pass a callback to the delay function, all we need are the seconds. We're gonna use a promise object to actually handle our callback…

Contents