JavaScript asynchronous call and javascript asynchronous call
Problem
The following aa () function can be modified to output the want-value using console. log ().
function aa() { setTimeout(function() { return "want-value"; }, 1000);}
However, there are additional requirements:
The aa () function can be modified at will, but cannot have console. log ()
The setTimeout package cannot exist in the console. log () statement.
Answer
Maybe this is an interview question. The main purpose of the problem is to check the processing of the asynchronous call execution result. Since it is an asynchronous call, it is impossible to wait for the asynchronous result synchronously, and the result must be asynchronous.
SetTimeout () is often used to simulate asynchronous operations. Earliest, asynchronous calls are used to notify (CALL) the processing result of the processing program.
function aa(callback) { setTimeout(function() { if (typeof callback === "function") { callback("want-value"); } }, 1000);}aa(function(v) { console.log(v);});
However, when callback is used for a slightly larger asynchronous application, it is prone to multi-layer nesting. Therefore, we propose to "flat" it, for details about this part, refer to "flattening" Asynchronous calls. Of course, Promise is a very popular method and is eventually adopted by es6. Use Promise to implement the following:
function aa() { return new Promise(resolve => { setTimeout(function() { resolve("want-value"); }, 1000); });}aa().then(v => console.log(v));
In this example, it is similar to the previous callback example. However, it will lead to a more recommended method-async/await, which is supported from ES2017:
function aa() { return new Promise(resolve => { setTimeout(function() { resolve("want-value"); }, 1000); });}async function main() { const v = await aa(); console.log(v);}main();
The definition of aa () is the same as that in the Promise method, but await is used during the call, and the console is used after waiting for the asynchronous result. log.
Note that await can only be used in the async method. Therefore, to use await, you must define an async main method and call it in the global scope. Because the main method is asynchronous (declared as async), if other statements such as console. log ("hello") exist after the main () method is called, this statement will be executed first.
The async/await syntax allows asynchronous calls to be written like Synchronous Code. When writing code, you can avoid logical jumps and make it easier to write. (Reference: from hell to heaven, Node callback changes to async/await)
Of course, you can use IIFE to encapsulate the definition of main () and then call main,
(async () => { const v = await aa(); console.log(v);})();
Summary
The above is the JavaScript asynchronous call introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!