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
Preface This article introduces a tutorial on how...
This article shares the specific code of JavaScri...
When faced with a SQL statement that is not optim...
After entering yum in linux, the prompt: -bash: /...
There are many form elements. Here is a brief sum...
Achieve results First use HTML to build a basic f...
What is Vite? (It’s a new toy on the front end) V...
Table of contents Preface 1. MySQL enables SSL co...
Table of contents Install Docker-ce for the devel...
The default time type (datetime and timestamp) in...
By right-clicking the source file, the following c...
When shutting down the MySQL server, various prob...
Table of contents Overview What is Big O notation...
Table of contents Preface Related Materials Vue p...
Installation Environment Centos Environment Depen...