new Promise((resolve) => setTimeout(resolve, ms))
resolve is the function that tells a JavaScript Promise : “This async task is finished successfully.” In your code: const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); here’s what happens step-by-step: new Promise(...) creates a promise. JavaScript automatically provides a function called resolve . setTimeout(resolve, ms) waits ms milliseconds. After the timer finishes, resolve() is called. Calling resolve() marks the promise as completed. So this: delay(1000) creates a promise that resolves after 1 second. Example: delay(1000).then(() => { console.log("1 second passed"); }); or with async/await : await delay(1000); console.log("done"); You can think of it like this: new Promise((resolve) => { // do async work resolve(); // finish successfully }); resolve is just a parameter name. You could rename it: new Promise((finish) => { setTimeout(finish, ms); }); but resolve is the standard convention. https://...
Comments
Post a Comment