async/await 是ES7引入的新語法,能夠更加方便的進行異步操做
async 關鍵字用於函數上(async函數的返回值是Promise實例對象)
await 關鍵字用於async函數當中(await能夠獲得異步的結果)
async function queryData (id) { const res = await axios.get('/data') return res.data } queryData.then(data => { console.log(data) })
async function queryData () { const info = await axios.get('/async1') const res = await axios.get('async2?info=' + info.data) return res } queryData.then(res => { console.log(res) })