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:
- SCSS use / deep / error, the latest usage: v-deep
- error Parsing error: Unexpected reserved word ‘await‘.
- Error in v-on handler (Promise/async): “[object Object]“
- SqlSession was not registered for synchronization because synchronization is not active
- Promise. All cooperates with await to implement synchronous execution of multiple requests
- Python: Understanding__ str__
- Simple understanding and basic operation of mongodb
- Servicetask synchronization failed to set automatic retrying
- Error in v-on handler (Promise/async): “Error: Request failed with status code 500“
- Asynchronous loading JS does not allow the use of document write solution
- The function of flatten layer in deep learning
- Design of MQ asynchronous collection report data
- Asynchronous Future Parallel processing request code example
- Rsync client synchronization error
- OpenGL programming error analysis
- Summary of Python deep learning packages
- Canal synchronization error target column: name not matched
- Errno in Linux Programming
- Deep learning: derivation of sigmoid function and loss function