I knew before that to synchronously obtain the results of asynchronous execution in So I made a concerted effort to solve this problem. As a programmer who often takes pride in reinventing the wheel, I think it is my responsibility to make a small contribution to the change of Consider 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('中文') console.log('c', c) This code is probably the best solution I could come up with to solve the asynchronous problem, but the answer is still unsuccessful. But I finally understand why in Look at where the translate method is called and print the translation result after the call. The purpose of doing this is, of course, that I hope to print it after there is a result. If In order to achieve the purpose of printing after the result is available, I cleverly encapsulated an Why? Because I forgot that By introducing the concept of scope, This concludes the article about Can await be omitted in JavaScript asynchrony? For more information about asynchrony in JavaScript, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: About the problem of vertical centering of img and span in div
>>: Loading animation implemented with CSS3
Environment Introduction Operating system: centos...
This article example shares the specific code of ...
Inside the style tag of the vue component, there ...
Table of contents Introduction Log classification...
If you are looking to monitor your system interac...
In the previous article, we wrote about how to de...
This article does not have any quibbles, it is jus...
top command is the best command that everyone is ...
This article shares the specific code of the WeCh...
vue-element-admin import component encapsulation ...
Blank's blog: http://www.planabc.net/ The use...
Table of contents Native JS How to send a get req...
Table of contents Conditional compilation Page La...
There are two types of html tags, inline elements...
This article mainly summarizes some commonly used...