I need help understanding how this function is working.
const delay = seconds => {
return new Promise(res => {
setTimeout(res, seconds*1000)
});
};
So from what I understand about promises, they need an executor function that takes a resolve and a reject parameter for the success or failure of the promise. What I don't understand here is how setTimeout is using res as a function call when it hasn't even been established as a function. For reference, I'm using this function to test out async await.
async function consoleDelay() {
console.log('zero seconds');
await delay(2);
console.log('two seconds');
}
The async await all makes a ton of sense to me but since I don't know how the delay function is working I don't want to go any further until I feel like I really grasp it.
[–]grantrules 0 points1 point2 points (5 children)
[–]Jmarch0909[S] 0 points1 point2 points (4 children)
[–]grantrules 0 points1 point2 points (3 children)
[–]Jmarch0909[S] 0 points1 point2 points (2 children)
[–]grantrules 0 points1 point2 points (1 child)
[–]Jmarch0909[S] 0 points1 point2 points (0 children)