Get the App
SLTechnology News&Howtos  ›  Development  › 

Why can't await be omitted from asynchronism in JavaScript

Shulou Source: shulou.com Published: 2022-06-02 05:32:53 10月04日 Update

This article mainly explains "why asynchronism in JavaScript can not omit await". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn why asynchrony in JavaScript can not omit await.

I knew before that to get the results of asynchronous execution synchronously in JavaScript, you must have await; in the for loop. If you don't use Promise.All, asynchrony won't work. But every time I need to wait for the implementation result, I need to bring async and await. I think it's always troublesome, and it doesn't look good. If you remember the spelling of these two words is good, do not remember each misspelling, can interrupt the train of thought, suddenly emerged such a word, also not beautiful.

So I worked together to solve this problem, and as a programmer who is often proud of building wheels, I think I have a responsibility to make a small contribution to the change of JavaScript.

Take a look at the following code:

Const trans = require ('node-google-translate-skidz'); function translate (str, strEn, tarEn) {let p = () = > {return new Promise ((resolve, reject) = > {trans ({text: str, source: strEn? StrEn: 'zh', target: tarEn? TarEn: 'en'}, function (result) {resolve (result.translation)});} let an = async () = > {let b = await p () console.log (b);} return an () / / console.log (b)} let c = translate (' Chinese') console.log ('Chinese, c)

This code is probably the best solution I can come up with to solve the asynchronous problem, and the answer is still unsuccessful. But I finally know why async and await can't really be avoided in JavaScript, no matter how much you package it.

Look at the place where the translate method is called and print the result of the translation after the call. The purpose of this, of course, is that I want the result to be printed. If the result is printed as Promise {}, it means it will be executed without waiting.

In order to print the result later, I cleverly encapsulated an async () = > {await} in the translate method, and then asked translate to return the result executed by await. But this method won't work, because the final printed result is still something I want to try my best to avoid.

Why? Because I forgot that async only works on function, which means that async has scope. After I executed the method let c = translate ('Chinese'), because I didn't tell the compiler that translate needed await (I thought async and await had been implemented inside translate, then translate would automatically wait for the execution result to return. But because of the scope, the scope within translate cannot affect this level of translate method execution.) So the compiler directly executes console.log ('centering, c), that is, prints Promise {}.

By introducing the concept of scope, the async and await of the method body cannot make the method call level also async, await, and you can probably understand why await cannot be omitted asynchronously in JavaScript.

Thank you for your reading, the above is the content of "why async in JavaScript can not omit await". After the study of this article, I believe you have a deeper understanding of why async in JavaScript can not omit await. The specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Results methods functions problems learning that is code content words ideas purpose compiler Chinese compilation consistency beauty pride do your best be smart work together Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Microsoft Huawei NVidia