async/await是一种编写异步代码的新方法,以前编写异步代码的方案是回调和promisehtml
async/await其实是创建在promise的基础上,它不能与普通回调或者node回调一块儿用node
async/await像promise同样,也是非阻塞的promise
async/await让异步代码看起来、表现起来更像同步代码,这正是其威力所在异步
假设函数getJSON
返回一个promise
,而该promise
的完成值是一个JSON对象,咱们只想调用它,并输出该JSON,而后返回“done”async
promise的实现函数
const makeRequest = ()=>{ getJSON().then( data => { console.log(data) return "done" } ) } makeRequest()
async、await的实现调试
const makeRequest = async ()=>{ console.log(await get.JSON()) return "down" } makeRequest()
async、await与Promise的区别code
函数前面有一个关键字 async。await 关键字只用在用 async 定义的函数内。全部 async 函数都会隐式返回一个 promise,而 promise 的完成值将是函数的返回值(本例中是 "done")。htm
上面一点暗示咱们不能在代码的顶层用 await,由于这样就不是在 async 函数内。对象
await getJSON() 意味着 console.log 调用会一直等待,直到 getJSON() promise 完成并打印出它的值。
咱们没必要写 .then,建立一个匿名函数来处理响应,或者给不须要用的变量一个名称 data。咱们还避免了代码嵌套。这些小小的优点会快速累积起来,在后面的代码中会变得更明显。
async/await 会最终让咱们用一样的结构( try/catch)处理同步和异步代码变成可能。在下面使用 promise 的示例中,若是 JSON.parse 失败的话,try/catch 就不会处理,由于它是发生在一个 prmoise 中。咱们须要在 promise 上调用 .catch,而且重复错误处理代码。这种错误处理代码会比可用于生产的代码中的 console.log 更复杂。
const makeRequest = () => { try { getJSON().then( result => { // 这里发生错误 const data = JSON.parse(result) console.log(data) }).catch((error)=>{ console.log(error) }) } catch (err) { // 这里不会捕捉到err console.log(err) } }
如今看看用 async/await 实现的代码。如今 catch 块会处理解析错误。
const makeRequest = async () => { try { // 这个解析会失败 const data = JSON.parse(await getJSON()) console.log(data) } catch (err) { console.log(err) } }
假设想作像下面的代码同样的事情,获取一些数据,并决定是否应该返回该数据,或者根据数据中的某些值获取更多的细节。
const makeRequest = () => { return getJSON() .then(data => { if (data.needsAnotherRequest) { return makeAnotherRequest(data) .then(moreData => { console.log(moreData) return moreData }) } else { console.log(data) return data } }) }
这些代码看着就让人头疼。它只需将最终结果传播到主 promise,却很容易让咱们迷失在嵌套( 6 层)、大括号和返回语句中。
把这个示例用async / await 重写,就变得更易于阅读。
const makeRequest = async () => { const data = await getJSON() if (data.needsAnotherRequest) { const moreData = await makeAnotherRequest(data); console.log(moreData) return moreData } else { console.log(data) return data } }
你可能发现本身处于一种状态,即调用你 promise1,而后用它的返回值来调用promise2,而后使用这两个 promise 的结果来调用 promise3。你的代码极可能看起来像这样:
const makeRequest = () => { return promise1() .then(value1 => { // do something return promise2(value1) .then(value2 => { // do something return promise3(value1, value2) }) }) }
不过用 async/await 的话,一样的逻辑就变得超级简单直观了。
const makeRequest = async () => { const value1 = await promise1() const value2 = await promise2(value1) return promise3(value1, value2) }
最后可是一样重要的是,在使用 async/await 时,一个杀手级优点是调试更容易。调试 promise 一直是如此痛苦,有两个缘由:
无法在返回表达式(无函数体)的箭头函数中设置断点。
const makeRequest = () =>{ return callAllPromise() .then(()=> callAllPromise()) .then(()=> callAllPromise()) .then(()=> callAllPromise()) .then(()=> callAllPromise()) }
若是在.then块中设置断点,并使用像单步调试这类调试快捷方式,调试器不会移动到后面的 .then ,由于它只单步调试同步代码。
有了 async/await,咱们就再也不须要那么多箭头函数,您能够像正常的同步调用同样单步调试 await 调用。
const makeRequest = async ()=>{ await callAllPromise() await callAllPromise() await callAllPromise() await callAllPromise() }