JavaScript Async Await
2020-04-04 本文已影响0人
sean_lau
image.png
image.png
image.png
image.png
image.png
代码:
function makeRequest(location){
return new Promise((resolve,reject)=>{
console.log(`Making Request to ${location}`)
if(location==='Google'){
resolve('Google says hi')
}else{
reject('we can only talk to Google')
}
})
}
function processRequest(response){
return new Promise((resolve,reject)=>{
console.log('processing response')
resolve(`Extra Information + ${response}`)
})
}
makeRequest('Google').then(response=>{
console.log('Response Received')
return processRequest(response)
}).then(processedResponse=>{
console.log(processedResponse)
}).catch(err=>{
console.log(err)
})
function makeRequest(location){
return new Promise((resolve,reject)=>{
console.log(`Making Request to ${location}`)
if(location==='Google'){
resolve('Google says hi')
}else{
reject('we can only talk to Google')
}
})
}
function processRequest(response){
return new Promise((resolve,reject)=>{
console.log('processing response')
resolve(`Extra Information + ${response}`)
})
}
// makeRequest('Facebook').then(response=>{
// console.log('Response Received')
// return processRequest(response)
// }).then(processedResponse=>{
// console.log(processedResponse)
// }).catch(err=>{
// console.log(err)
// })
async function doWork(){
try{
const response=await makeRequest('Facebook')
console.log('Response Received')
const processedResponse=await processRequest(response)
console.log(processedResponse)
}catch(err){
console.log(err)
}
}
doWork()