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
Table of contents Join syntax: 1. InnerJOIN: (Inn...
Table of contents 1. Get to know Teleport 2. Basi...
<br />In the field of network design, resear...
Open the connection tool. I use MobaXterm_Persona...
In this experiment, we configure MySQL standard a...
Reference: Docker official redis documentation 1....
Table of contents Animation Preview Other UI Libr...
Background description: On an existing load balan...
1. Download jdk download address我下載的是jdk-8u221-li...
Table of contents Phenomenon Root Cause Analysis ...
Talk about the scene Send Email Embedding HTML in...
Many people have encountered this error when star...
This article introduces how to install the system...
This article example shares the specific code of ...
First run the docker container Run the command as...