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
In order to make the page display consistent betwe...
The method of obtaining the position of the point...
Create a test table -- --------------------------...
Table of contents 1. Introduction 1. What is an i...
Method 1: Download Pycharm and install Download a...
1. RTMP RTMP streaming protocol is a real-time au...
illustrate In front-end development, you often en...
1. Get is used to obtain data from the server, wh...
Table of contents Introduction to WiFi Wireless T...
0. Prepare relevant tables for the following test...
The installation and configuration method of MySQ...
1. Preparation Install Tomcat on Linux system, us...
1. Prepare data The following operations will be ...
Preface: After studying the previous article, we ...