Promise async await

165 阅读1分钟

async函数返回一个promise对象,可以使用then方法添加回调函数

async function getStockPriceByName(name) {
  var symbol = await getStockSymbol(name);
  var stockPrice = await getStockPrice(symbol);
  return stockPrice;
}

getStockPriceByName('goog').then(function (result){
  console.log(result);
});

function timeout(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    })
}
async function hhh(value) {
    await timeout(3000)
    console.log(value)
}
hhh('hello')