Check out a free preview of the full Interviewing for Front-End Engineers course:
The "Promises Solution" Lesson is part of the full, Interviewing for Front-End Engineers course featured in this preview video. Here's what you'd learn in this lesson:

Jem live codes the solution to the Promises exercise while also setting up and solving a bonus question.

Get Unlimited Access Now

Transcript from the "Promises Solution" Lesson

[00:00:00]
>> So if I wanted to make something sleep, that would mean I wanted to do something similar to the bounce. I want a function that's not gonna execute until some elapsed time. So at some point, I'm gonna use a set timeout, somewhere in there. But I wanna use a promise because we're using async await.

[00:00:21] So let's code that up. Close you. All right, so again, the method of tackling any problem is I need a function that takes time, and we're gonna call it sleep. So function sleep, and it's gonna take time. And I'm gonna make this long because it's, I'm gonna break this out like this.

[00:00:52] But essentially because we're using an async method, we know we wanna use a promise in this particular one. So I can say return a new Promise. And Promises always have, I always wanna call it a callback, but it's not a callback. It's called an executor, because it will eventually always execute this function.

[00:01:17] And it's gonna have two, all Promises have resolve and reject. As in you set, hey, when this returned, is this valid or is not valid? But in this case, we're not gonna use reject. I just put that in there for giggles. So we have a Promise, we have a function.

[00:01:40] It's gonna return a Promise, and it's gonna resolve at some point. So the resolve to actually return, let's say I'm done processing, we have to invoke resolve. But we wanna wait x seconds, and how do we wait again?
>> Set a time out.
>> Yes, so I'm gonna say, setTimeout.

[00:02:01] Timeout, which of course takes some function. And we want setTimeout to run for the sleep second, so we want to run it at time. And whenever the time has elapsed, we're gonna resolve. And that is how you build a sleep function. So now, going back to our example, it's going to sleep, it's gonna wait.

[00:02:31] We're not waiting for any value. We don't care what the value is. We're gonna wait 500 milliseconds. Whatever we pass in, it`s gonna execute this. It's gonna hit this. It's gonna wait another 500 milliseconds. It's gonna print this. I consider this a pretty decent interview question. Cuz it's like, do you understand Promises, do you understand setTimeout?

[00:02:49] Do you understand how Promises get resolved? Without going into too much detail. Now, the other question I was gonna ask you, and I'm not gonna ask you this one cuz it's challenging, is, given some function that has a signature that finishes the callback. So kind of like old-school JavaScript, or a lot of modern node functions, you can do this, too.

[00:03:09] How would I promisefy this function so that I can say, .then, .then, .then, .then? I'm not gonna go into that one because it would take a while to go over, but I can show you the solution real fast. [SOUND] Answers. And when you see it, you're gonna groan.

[00:03:30] You'll be like, why, Jem? Why would you do that to us? A little messy, but similar idea. You have a function that's gonna eventually return a function, and then when you invoke that, it's gonna be a Promise, so you can chain it. So it's a function that takes a function that returns another function and returns a Promise.

[00:03:50] That Promise is just gonna resolve, and then you call that. And you concat all the arguments together, with the last one being the callback function. Just like you would. So it calls the callback once the promise resolves. It's more of a mental model of how you would do this that I find more important than actually understanding the nitty-gritty of it.

[00:04:10] But ignore that you saw that. Go ahead and try to promisefy something, because it is an interview question people will ask you. And it's probably one of people's least favorite things. Because you just, I never thought, how would I promisefy something? I never gave it any thought. So go ahead and try that one at home when you have a chance.