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

前端 未结 11 1712
执念已碎
执念已碎 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-30 00:04

    When Node 0.11 (with v8 3.19 [1], which has generators[2]) arrives, you can use Galaxy and code like below.

    However, only behind a flag. They are supported natively in ioJS.

    function* countLines(path) {
        var names = yield fs.readdir(path);
        var total = 0;
        for (var i = 0; i < names.length; i++) {
            var fullname = path + '/' + names[i];
            if ((yield fs.stat(fullname)).isDirectory()) {
                total += yield countLines(fullname);
            } else {
                var count = (yield fs.readFile(fullname, 'utf8')).split('\n').length;
                console.log(fullname + ': ' + count);
                total += count;
            }
        }
        return total;
    }
    
    function* projectLineCounts() {
        var total = 0;
        total += yield countLines(__dirname + '/../examples');
        total += yield countLines(__dirname + '/../lib');
        total += yield countLines(__dirname + '/../test');
        console.log('TOTAL: ' + total);
        return total;
    }
    
    0 讨论(0)
  • 2020-12-30 00:04

    Javascript is providing async-await feature with ECMA 7. Now all asynchronous function can be awaited by promisifying them and waiting for promise to resolve. Most of the asynchronous functions like DB calls, API calls, fs and events are returning promise now in Javascript and nodeJs. Now with async-await code is more cleaner, understandable, debugged.

    Example

    function timeout(){
      return new Promise( resolve => {
        setTimeout(function(){
          resolve(true);
        }, 5000);
      });
    }
    
    async function f(){
        let result = await timeout();
    }
    
    0 讨论(0)
  • 2020-12-30 00:08

    Async is on feature list for JavaScript harmony. So far there are numerous attempts to provide such functionality in the browser or in node, none of them seem to be compatible with harmony proposal though:

    • Async can be simulated with JS1.7 generators (see task.js). Not yet supported out-of-the-box by V8 (without experimental mode), but works in FF. Possibly traceur or Masacra compiler can be used to bring generators to other environments.
    • There is node-fibers library providing other mechanism for asynchronous programming in node (scarifies performance though). Other attempt basing on v8cgi is described here.
    • Rhino has continuations out of the box providing good alternative. This is why Ringo.js might be worth looking at.
    • Few solutions basing on js2js translation are available, e.g: jscx, NarrativeJS, jwacs, StratifiedJS. Some support integration with node.
    • There are many promise/future libraries trying to solve callbacks problem without extending syntax, however they all suffer from composability issues, i.e. cannot use language constructs like loops across callbacks.
    0 讨论(0)
  • 2020-12-30 00:08

    async/await looks to be coming in ECMAScript 7. This proposal was accepted into stage 1 of the specification process in January 2014.

    The good news is that Googles traceur compiler already supports it, so you could start using it today.

    Sample syntax:

    async function asyncValue(value) {
      await timeout(50);
      return value;
    }
    

    async/await is also on the TypeScript roadmap.

    0 讨论(0)
  • 2020-12-30 00:08

    If you are interested in .NET style asynchronous programming for JavaScript you should look into Rx for JavaScript. Rx for JavaScrpt is Microsoft's JavaScript port of the Reactive Framework. The reactive framework is described as:

    A library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.

    You can download Rx for JavaScript here

    And you can read more about it, including examples here

    You can also install it on node with npm:

    npm install rx

    It works well with libraries like jQuery, however I am not a CoffeeScript programmer, so I'm not sure what support there is for interoperability with other JavaScript libraries in this language.

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