I’m new to Javascript and trying to understand asynchronous mechanism in Javascript.
I learnt that promise good solution for callback problem.
It’s constructor takes a callback function called executer. And the executer function takes 2 function, resolve and reject. The problem for me starts here.
I never define these functions but I use them in executer function. I can’t understand why I pass these two functions to executer. I think these functions are defined in somewhere by the javascript. If it is, why do we not use them like other library functions like map, forEach?
Here is the example code that use promise function created by Microsoft Copilot
function printNumbers2(start, end) {
return new Promise((resolve, reject) => {
function printNext(current) {
if(current > end){
resolve();
return;
}
console.log(current);
setTimeout(() => printNext(current + 1), 0);
}
printNext(start);
})
}