1. async How to turn it into an asynchronous function? It starts with the keyword async function f() { return 1; } f().then(alert); // 1 //The results are the same as above async function f() { return Promise.resolve(1); } f().then(alert); // 1 //You can also use arrow function let hello = async () => { return "1" }; hello().then((value) => console.log(value)) //The return value can also be simplified like this hello().then(console.log) One of the characteristics of asynchronous functions: the return value of the function is guaranteed to be Adding the 2. await: You can use async function f() { let promise = new Promise((resolve, reject) => { setTimeout(() => resolve("boom!"), 1000) }); let result = await promise; // Wait until promise resolves. alert(result); // "Boom!" } f(); //Get result and continue to execute. So the above code displays "Boom!" after 1 second.
3. Comprehensive Application With async function A() { let response = await fetch('c.jpg'); let myBlob = await response.blob(); let objectURL = URL.createObjectURL(myBlob); let image = document.createElement('img'); image.src = objectURL; document.body.appendChild(image); } A() .catch(e => { console.log('Problem: ' + e.message); }); You wrap your code with fewer This is the end of this article about how to use JS You may also be interested in:
|
<<: Docker case analysis: Building a MySQL database service
How to obtain SQL statements with performance iss...
Nginx is now one of the most popular load balance...
1. Introduction to Data Integrity 1. Introduction...
Building an image is a very important process in ...
zabbix_agent deployment: Recommendation: zabbix_a...
Table of contents this Method In the object Hidde...
Preface: Sometimes in a route, the main part is t...
1. Introduction This article mainly explains how ...
Table of contents Overview Environment Preparatio...
Give time time and let the past go. In the previo...
The installation tutorial of mysql 8.0.11 winx64 ...
Content 1. Give readers a reason to stay. Make the...
The shutdown.bat file has a sentence if not "...
mysql create table sql statement Common SQL state...
When browser vendors bend the standards and take i...