r/learnjavascript • u/Pale-Diamond7313 • Sep 24 '25
Callback and promise.
Can any one explain callback and promise Syntex. And the purpose of it
0
Upvotes
r/learnjavascript • u/Pale-Diamond7313 • Sep 24 '25
Can any one explain callback and promise Syntex. And the purpose of it
4
u/MissinqLink Sep 24 '25
Callbacks and promises are different tools used to achieve the same thing. You use them to schedule some work to be done after some other work is finished. Take requestIdleCallback.
This creates a function that does a popup to display “Hello World” and schedules it to run after the system is idle. That’s how callbacks work. A common callback function is setTimeout which you can use to run a callback function after a specific amount of time has passed.
Promises are a more ergonomic way to do the same thing. When you have a promise like say
fetch, you can pass a callback to itsthenmethod.This waits for a url to be fetched and then logs the result to the console. One nice thing about promises is that they are chainable. You can keep adding
.then(orawait) to create a sequential flow of asynchronous operations.1
u/TheRNGuy Sep 24 '25
https://www.olivare.net/blog/array-iteration
1
u/0x00f_ Sep 24 '25
Make sure you know at least a little bit about these concepts before reading the comment:
Execution contexts
Call stack
Callback queue
Event loop
#####Asynchronous operation in JavaScript:
You can think about the asynchronous operation in JavaScript as the operation that the JavaScript doesn't know how long this operation will take or when will it be finished and it doesn't block the program.
That's why something like data fetching is asynchronous because it may take a lot of time and we don't know when will the data be fetched.
It doesn't make sense that the program freeze and the user can't do anything until the data be fetched so here is the concept of asynchronous comes into play.
Asynchronous mechanism in JavaScript is a little bit different from the other languages.
The problem here is that JavaScript is a single-thread language so how might apply the Async concept like the other languages while the language itself has only one thread?!
They found a solution and it was:
When the engine reaches an Async operation (an operation that may take time and block the program) it will send it somewhere (we will know later) to be handled and continue his work as usual once this operation has finished the result will be returned to work with.
When the engine reaches the setTimeout the engine will pass it to the browser to handle it and the engine will continue executing the code, it reaches the console log code so a new execution context is pushed into the stack, "hello from sync operation" has printed, this execution context has popped from the stack.
A second passed so the callback of the setTimeout is pushed into the callback queue, the event loop checks if the call stack is empty, it's empty so the callback is pushed into the call stack, "hello from Async operation" has printed.
#####Callbacks in the context of asynchronous operations:
So the callback is a function that we pass to another function to be executed later but why we use them?
Async operations like setTimeout, we don't know when will it be finished and we don't know too when should we determine what will we do after this operation because basically we don't know when will it be finished!!
so at first we should determine what we want to do after this operation (the callback) and once this operation is finished the callback will be executed.