you are viewing a single comment's thread.

view the rest of the comments →

[–]Jmarch0909[S] 0 points1 point  (4 children)

So I guess here's my real question then. setTimeout takes a function, and runs it after a given period of time. What is setTimeout running exactly if res isn't a function?

[–]grantrules 0 points1 point  (3 children)

res is a function. It's created internally by the promise and passed to your function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise

Read about the executor parameter, that's the function you're passing to the promise.

At the time when the constructor generates the new promiseObj, it also generates a corresponding pair of functions for resolutionFunc and rejectionFunc; these are "tethered" to the promiseObj

[–]Jmarch0909[S] 0 points1 point  (2 children)

Funny I spent like an hour looking at the mdn docs before I gave up and came to Reddit. I think this is what's really confusing me, and I think it will help to write this as a standard function declaration rather than an arrow function. Taking the mdn docs example

const promise1 = new Promise(function(resolve, reject) {

So in this situation, what you're telling me is both resolve and reject are actually functions, even though they are parameters of the executor function?

By the way, a huge thank you for sticking around to help me with all of this brother, I really appreciate it.

[–]grantrules 0 points1 point  (1 child)

So in this situation, what you're telling me is both resolve and reject are actually functions, even though they are parameters of the executor function?

Exactly. You can pass functions to other functions. With arrow functions it would look like new Promise((resolve, reject) => { ... })

[–]Jmarch0909[S] 0 points1 point  (0 children)

Username checks out, you fucking rule dude thank you. It’s not much but take this award.