Monday 27 February 2017

Why is 'async' required for functions that use 'await'?

I am working on a new project that uses Node 7.6.0 (with the new await/async) and node-mssql which is a Promise-based library.Several of the node-mssql methods return a Promise, so I can now use 'await' to have my code effectively pause & wait for the Promise to resolve (or fail) before moving on to execute the next line (or catch block).I like everything about this arrangement, except for the part where I have to declare my functions with "async" in order to be able to use the "await" keyword inside of it.I find myself writing code wrapped in an anonymous function, just to declare "async" so that I can use "await":(async function(){ await MyLibrary.SomePromiseMethod(); })(); My question is, why is the "async" keyword required? Is there a technical necessity to it? I'm trying to understand why "await" isn't enabled & available by default.

Submitted February 27, 2017 at 07:06PM by ninjaroach

No comments:

Post a Comment