日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

發布時間:2025/3/21 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9

----------------------------------------------------------------------------------------------

6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)

In case you missed it, Node now supports async/await out of the box since version 7.6. If you haven’t tried it yet, here are a bunch of reasons with examples why you should adopt it immediately and never look back.

[UPDATE]:?Node 8 LTS?is out now with full Async/Await support.

[EDIT]: It seems that the embedded code on gist does not work on medium native app, but it works on mobile browsers. If you are reading this on the app, tap on the share icon and choose “open in browser” in order to see code snippets.

Async/await 101

For those who have never heard of this topic before, here’s a quick intro

  • Async/await is a new way to write asynchronous code. Previous options for asynchronous code are callbacks and promises.
  • Async/await is actually built on top of promises. It cannot be used with plain callbacks or node callbacks.
  • Async/await is, like promises, non blocking.
  • Async/await makes asynchronous code look and behave a little more like synchronous code. This is where all its power lies.

Syntax

Assuming a function?getJSON?that returns a promise, and that promise resolves with some JSON object. We just want to call it and log that JSON, then return?"done".

This is how you would implement it using promises

const makeRequest = () =>getJSON().then(data => {console.log(data)return "done"})makeRequest()

  

And this is how it looks with async/await

const makeRequest = async () => {console.log(await getJSON())return "done" }makeRequest()

  

There are a few differences here

  • Our function has the keyword?async?before it. The?await?keyword can only be used inside functions defined with?async.?Any?async?function returns a promise implicitly, and the resolve value of the promise will be whatever you?return?from the function (which is the string?"done"?in our case).
  • The above point implies that we can’t use await in the top level of our code since that is not inside an?async?function.
  • 3.?await getJSON()?means that the?console.log?call will wait until?getJSON()?promise resolves and print it value.

    Why Is It?better?

    1. Concise and?clean

    Look at how much code we didn’t write! Even in the contrived example above, it’s clear we saved a decent amount of code. We didn’t have to write?.then, create an anonymous function to handle the response, or give a name?data?to a variable that we don’t need to use. We also avoided nesting our code. These small advantages add up quickly, which will become more obvious in the following code examples.


    2. Error?handling

    Async/await makes it finally possible to handle both synchronous and asynchronous errors with the same construct, good old?try/catch. In the example below with promises, the?try/catch?will not handle if?JSON.parsefails because it’s happening inside a promise. We need to call?.catch?on the promise and duplicate our error handling code, which will (hopefully) be more sophisticated than?console.log?in your production ready code.

    Now look at the same code with async/await. The?catch?block now will handle parsing errors.


    3. Conditionals

    Imagine something like the code below which fetches some data and decides whether it should return that or get more details based on some value in the data.

    Just looking at this gives you a headache. It’s easy to get lost in all that nesting (6 levels), braces, and return statements that are only needed to propagate the final result up to the main promise.

    This example becomes way more readable when rewritten with async/await.


    4. Intermediate values

    You have probably found yourself in a situation where you call a?promise1and then use what it returns to call?promise2, then use the results of both promises to call a?promise3. Your code most likely looked like this

    If?promise3?didn’t require?value1?it would be easy to flatten the promise nesting a bit. If you are the kind of person who couldn’t live with this, you could wrap both values 1 & 2 in a?Promise.all?and avoid deeper nesting, like this

    This approach sacrifices semantics for the sake of readability. There is no reason for?value1?&?value2?to belong in an array together, except to avoid nesting promises.

    This same logic becomes ridiculously simple and intuitive with async/await. It makes you wonder about all the things you could have done in the time that you spent struggling to make promises look less hideous.


    5. Error?stacks

    Imagine a piece of code that calls multiple promises in a chain, and somewhere down the chain an error is thrown.

    The error stack returned from a promise chain gives no clue of where the error happened. Even worse, it’s misleading; the only function name it contains is?callAPromise?which is totally innocent of this error (the file and line number are still useful though).

    However, the error stack from async/await points to the function that contains the error

    This is not a huge plus when you’re developing on your local environment and have the file open in an editor, but it’s quite useful when you’re trying to make sense of error logs coming from your production server. In such cases, knowing the error happened in?makeRequest?is better than knowing that the error came from a?then?after a?then?after a?then?…


    6. Debugging

    Last but not least, a killer advantage when using async/await is that it’s much easier to debug. Debugging promises has always been such a pain for 2 reasons

  • You can’t set breakpoints in arrow functions that return expressions (no body).
  • Try setting a breakpoint anywhere?here

    2. If you set a breakpoint inside a?.then?block and use debug shortcuts like step-over, the debugger will not move to the the following?.then?because it only “steps” through synchronous code.

    With async/await you don’t need arrow functions as much, and you can step through await calls exactly as if they were normal synchronous calls.


    In Conclusion

    Async/await is one of the most revolutionary features that have been added to JavaScript in the past few years. It makes you realize what a syntactical mess promises are, and provides an intuitive replacement.

    Concerns

    Some valid skepticism you might have about using this feature

    • It makes asynchronous code less obvious: Our eyes learned to spot asynchronous code whenever we see a callback or a?.then, it will take a few weeks for your eyes to adjust to the new signs, but C# had this feature for years and people who are familiar with it know it’s worth this minor, temporary inconvenience.
    • Node 7 is not an LTS release: Yes, but node 8 is coming next month, and migrating you codebase to the new version will most likely take little to no effort. [UPDATE]: Node 8 LTS is now out.

    轉載于:https://www.cnblogs.com/oxspirt/p/9768060.html

    總結

    以上是生活随笔為你收集整理的【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 亚洲免费网站在线观看 | 青青草国产在线播放 | 久久两性视频 | 97播播| 久久久视屏 | 手机av中文字幕 | 色婷婷五 | 手机看片国产1024 | av福利网| 朝桐光av一区二区三区 | 波多野吉衣av在线 | 麻豆日产六区 | 久久久精品人妻av一区二区三区 | 欧美日韩国产高清视频 | 亚洲AV无码阿娇国产精品 | 超碰伦理 | 久久精品中文字幕 | 亚洲一区二区三区91 | 337p日本欧洲亚洲大胆精筑 | 一本大道av伊人久久综合 | 日韩成人av一区 | 爆乳熟妇一区二区三区霸乳 | 国产福利在线观看 | 99热免费在线观看 | 亚洲国产精品成人va在线观看 | 一区二区国产精品精华液 | 看毛片的网站 | 久久无码人妻丰满熟妇区毛片 | 中文字幕第二页 | 亚洲福利一区 | 国产主播毛片 | 国产一区二区在线观看视频 | 一区二区三区四区在线播放 | 国产精品毛片久久久久久久av | 国产精品50页 | 中文字幕一区二区三区精彩视频 | 中文字幕在线播放 | 中文字幕另类 | 欧日韩不卡视频 | 椎名空在线 | 中文字幕一区二区三区夫目前犯 | 国产亚州av | 日本不卡在线播放 | 91拔萝卜视频 | 国产成人啪精品午夜在线观看 | 日韩精品一| 免费观看日韩av | 亚洲一区二区三区四区av | www网站在线免费观看 | 免费毛片一区二区三区久久久 | 中文精品视频 | 精品九九视频 | 97精品一区二区视频在线观看 | 成人拍拍拍 | 在线观看特色大片免费网站 | 国产女人和拘做受视频免费 | 影音av资源 | 卡通动漫av| 视频一区欧美 | 国产乱码在线 | 理伦毛片 | 国产精品免费av | 日本黄色生活片 | 精品不卡一区 | 四虎国产精品永久在线国在线 | 国产3p在线播放 | 青青艹在线视频 | 日韩网站免费 | 国产富婆一区二区三区 | 美女福利在线观看 | 黄色成人在线网站 | 激情综合网五月婷婷 | 中文字幕日韩亚洲 | 七月色 | 免费av在线网站 | 伊人久久大香线蕉av色婷婷色 | 色乱码一区二区三区在线男奴 | 超碰97国产在线 | 男人操女人下面 | 久久精品无码av | 久久综合久久久 | 免费观看黄色一级视频 | 免费av网站在线 | 婷婷六月天在线 | 国产午夜精品在线 | 日韩欧美精品一区二区三区 | 男插女视频免费 | 电影桑叶2在线播放完整版 222aaa | 国产97色在线 | 国产 | 亚洲一区视频 | 秋霞在线一区二区 | 全程粗话对白视频videos | 黄视频在线 | 黄色网址你懂得 | 久久老司机 | 精品一性一色一乱农村 | 色婷婷综合网 | 丰满人妻一区二区三区精品高清 | 国产又粗又爽视频 |