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

前端 未结 11 1711
执念已碎
执念已碎 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:45

    good news,

    nodejs supports it from v7.0.0 (well, partially), still need a harmony flag --harmony_async_await, and apparently has some bugs including memory leak, for more details, but there are some concerns as well, and one commentator advises to wait till v8 version 55 which might not be long.

    0 讨论(0)
  • 2020-12-29 23:48

    I'm not familiar with C#, but it sounds like what you're looking for is some sort of continuations, so that instead of writing

    fs.readFile 'foo.txt', (err, data) ->
      myFunc data
    

    you could instead just write something like

    data = &fs.readFile 'foo.txt'  # not a real syntax
    myFunc data
    

    This isn't something that JavaScript or CoffeeScript provides. However, there are several other compilers that can do something like this:

    • TameJS - JavaScript-based, mainly just adds this feature
    • Kaffeine - JavaScript-based, adds a bunch of features
    • coco - CoffeeScript-based

    See also: List of languages that compile to JavaScript on the CoffeeScript wiki.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 23:50

    there is https://github.com/loveencounterflow/coffy-script which is the attempt to add yield to CoffeeScript. CoffyScript is very new and as i'm writing this i'm pondering over the difficulties posed by require.extensions being a global, and whether i should be a separate extension. that said, CoffyScript does work, and you will find on my github page numerous examples that show how to write quite succinct asynchronous code using suspend, resume, and yield.

    given that yield has arrived in NodeJS 11.2, i believe we should research how to use generators / coroutines to make asynchronous programming more palatable. i for one have tossed out promises, the experience with them having not been so pleasant. Then again, it may take a while before yield becomes available in all major browsers.

    0 讨论(0)
  • 2020-12-29 23:51

    For completeness: I have found that Saltarelle Compiler (which actually compiles C# to JavaScript) also supports await/async.

    0 讨论(0)
  • 2020-12-29 23:53

    Yes there is, and you don't even need to compile it, because it is just a simple JavaScript library.

    One of my projects called sharpnr.js has the aim to extend JavaScript with great features of C# (and .NET of course) like await/async, or LINQ.

    The library is currently in beta, but it's stable and supports almost every statement (for example loops, switch, if), and works well with existing libraries (like jQuery).

    The await/async syntax is almost identical to the C# version:

    var getAsync = async(function(url) {
      var result = await; $.get(url);
      $("#test").html(result);
    });
    getAsync("http://www.sharpnrjs.com");
    

    Working example on jsfiddle.

    You can download the library from github.

    0 讨论(0)
提交回复
热议问题