In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.