we know that the JavaScript code executes sequentially, so let’s look at the result of the execution of the following code:
function a(){
return new Promise((resolve,reject)=>{
resolve("a")
})
}
function b(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
resolve("b")
}, 2000);
})
}
function c(){
return new Promise((resolve,reject)=>{
resolve("c")
})
}
function execute(){
let a1= a()
a1.then(res=>{
console.log(res);
})
let b1= b()
b1.then(res=>{
console.log(res);
})
let c1= c()
c1.then(res=>{
console.log(res);
})
}
execute()
so how to show it in order, that is, C wait for B to execute after the execution. The following p>
function a(){
return new Promise((resolve,reject)=>{
resolve("a")
})
}
function b(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
resolve("b")
}, 2000);
})
}
function c(){
return new Promise((resolve,reject)=>{
resolve("c")
})
}
async function execute(){
let a1=await a()
//用了await之后,就不用.then了直接得到结果
console.log(a1)
let b1=await b()
console.log(b1)
let c1=await c()
console.log(c1)
}
execute()
Read More:
- error Parsing error: Unexpected reserved word ‘await‘.
- VUEJS Failed to execute ‘removeChild’ on ‘Node’: The node to be removed is not a child of
- Encapsulation of Axios and management of API interface in Vue
- Error in v-on handler (Promise/async): “[object Object]“
- How to use JavaScript in HTML
- On and off of timer in JS
- Promise. All cooperates with await to implement synchronous execution of multiple requests
- How to Use Truffle to Deploy contracts on moonbeam
- Lock wait timeout exceeded — transaction and index
- Detailed explanation of yield in Python — the simplest and clearest explanation
- C#: How to Use Httpclient to upload files with other parameters
- C + + pauses the black window system (“pause”); (get ch(), getchar(), system (pause)’s connection and difference
- The problem that headers [‘content-type ‘] does not work is set in the Axios get method request interface
- Django + jQuery get data in the form + Ajax send data
- How to use C + + function pointer array
- Realization of breakpoint download based on DIO in flutter
- Vscode setting console. Log () shortcut method
- The echots in Vue reports an error. After obtaining the DOM element, the chart can be displayed. The console still reports an error
- Tainted canvases may not be exported
- If JavaScript exceeds the length of the array, no error will be reported