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

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

    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();
    }
    

提交回复
热议问题