I am new to JS and was trying to get how asynchronous callbacks work and how to properly use them but faced some confusion. So, as far as I understood asynchronous callback is executed only after the other code finished executing, that is, if we send request to server and we do not know when the data will be obtained then we need to use asynchronous callback which ensures that callback function is executed only after we get data. But what confuses me is that why we use asynchronous in front of callback since asynchronous is what is run via event loop but callback awaits until other code finishes and asynchronous callback is not run via event loop Right?
Asked
Active
Viewed 45 times
-1
-
A callback is a function you pass as argument to another function, usually one that starts an asynchronous processing and returns without waiting for that processing to complete (an HTTP request to a website, f.e). When the processing completes, your callback is executed. The callback can be a synchronous function (it usually is) but it can also start another asynchronous operation (and pass another callback to it and so on). Please note that I am not talking about the `async` keyword (which is not necessarily to be involved) you put before a function definition. – axiac Jul 23 '19 at 15:34
-
@axiac, I have only one question and I really hope you will help. So, imagine we have xhr code and also callback function that is executed only after we get data from server. The question is since xhr object is executed via event loop, do we need to put asynchronous callback into xhr so that that callback is passed together with xhr into WEB API then to queue then to be put into call stack. Is that the reason why callback is called asynchronous since it is together passed into web api then to queue. Hope you got what I mean:) – user11807902 Jul 23 '19 at 15:40
-
it's called asynchronous because it is not synchronized with the function that queued it. – Chris Rollins Jul 23 '19 at 15:47
-
@axiac, I mean, asynchronous callback is called asynchronous because it kinda travels together with other asynchronous code like xhr into WEB API ->callback queue-> call stack then inside call stack asynchronous callback is run after xhr code. Am I correct? – user11807902 Jul 23 '19 at 15:47
-
@ChrisRollins, hey Chris, I mean, asynchronous callback is called asynchronous because it kinda travels together with other asynchronous code like xhr into WEB API ->callback queue-> call stack then inside call stack asynchronous callback is run after xhr code. Am I correct? – user11807902 Jul 23 '19 at 15:48
-
Forget about the event loop, it only increases your confusion. When you call a function that starts an asynchronous operation, the function returns immediately, it does not wait for the operation to complete. Without a callback you cannot know when the asynchronous operation completes. This is its purpose, to notify your code when that operation is done. The callback is an event handler (similar to those that handle clicks) but the event it handles is not a click or a key press but the completion of the asynchronous operation. The event happens only once. – axiac Jul 23 '19 at 15:54
1 Answers
-1
asynchronous callbacks offer some guarantees in JS, what I mean by that is that although a piece of code may be running asynchronously with respect to the main code within this piece of code some order must exist, therefore JS will prevent any callback function from being executed before the completion of the current run of the the event loop. Therefore, this will prevent any callbacks from being executed before a promise is fulfilled for example and hence ensure a logical flow of execution.
you can check out these resources for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Run-to-completion https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
-
Asynchronous callback is called asynchronous because it kinda travels together with other asynchronous code like xhr into WEB API ->callback queue-> call stack then inside call stack asynchronous callback is run after xhr code. Am I correct? – user11807902 Jul 23 '19 at 15:51