How To Create Custom Asynchronous Function In Javascript?
Solution 1:
But how to create that natively without using any prebuilt function to achieve that?
You cannot.
I know that functions that query a database have an asynchronous behavior, how do they do that without using something like setTimeout() in their original implementation?
They usually will connect to the database using a network or filesystem socket, and for those there are builtin asynchronous functions (calling a callback when a response is received etc) that they build upon.
For node.js specifically, there is of course also the possibility of writing an addon to the engine itself that supplies such a "natively asynchronous" function to the JavaScript environment.
Solution 2:
Such functions usually aren't written in JavaScript.
They are usually written in the same programming language as the runtime engine and then plugged into it and exposed as a JavaScript function.
This is why if you were to run XMLHttpRequest.toString()
in a browser, you'll get "function XMLHttpRequest() { [native code] }"
and not the actual code of the function.
Post a Comment for "How To Create Custom Asynchronous Function In Javascript?"