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

前端 未结 11 1709
执念已碎
执念已碎 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;
    }
    

提交回复
热议问题