JavaScript Async Await

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()

你可能感兴趣的:(JavaScript Async Await)