Async/await in web browser or in node.js?

前端 未结 11 1801
执念已碎
执念已碎 2020-12-29 23:18

Is there any attempt to bring async/await feature from C# 5.0 to any language which can be compiled to JavaScript (such as CoffeScript)? (So it can be used

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 23:50

    You can have async/await in Google Chrome with Experimental JS flag enabled, using built-in Generators, Promises and a tiny spawn() function by Jake Archibald:

    spawn(function*() { //this function is async
          let story = yield getJSON('story.json'); //yield is like await
          addHtmlToPage(story.heading);
    });
    

    Alternatively, you can use:

    • Q with async() and spawn() by Kris Kowal
    • Co by Visionmedia
    • Awaitable by Jonathan Ong
    • Task.js by Mozilla
    • Then-yield by Forbes Lindesay
    • When by cujoJS

    For browsers not supporting ES6, there is Facebook Regenerator.

提交回复
热议问题