Is it safe to use async-await in Javascript instead of generators-promises now, knowing that the syntax has not made yet and will be coming with the release of ES8?
What browsers can I count on it being available, and how common are the browsers where this syntax is not available? By Safe I mean without some transpilers like babel?
Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
There are a few alternative async/await transpilers to Regenerator, which take async code and attempt to convert to more traditional . then and . catch notation. In my experience, these transpilers work pretty well for simple functions, which await then return , perhaps with a try/catch block at most.
Use of async and await enables the use of ordinary try / catch blocks around asynchronous code. Note: The await keyword is only valid inside async functions within regular JavaScript code. If you use it outside of an async function's body, you will get a SyntaxError .
The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment.
Just because your code is asynchronous doesn’t mean that it’s safe. Shared resources still need to be protected, and this is complicated by the fact that you can’t await from inside a lock. Here’s an example of async code that can corrupt shared state if it executes twice, even if it always runs on the same thread:
If you are using async await, it’d look like this: If you do that to all your async functions, then when using your code, it now looks like this: Then if all your functions look like this, there are no exceptions, and all functions agree to follow the same convention.
It makes promises easier to read and write. An async function is a function declared with the async keyword and the await keyword can only be used within an async function. The number of await keywords can vary from zero to as much as you need.
This behavior is inherent in all types of asynchronous programming, not just the new async/await keywords. “Async all the way” means that you shouldn’t mix synchronous and asynchronous code without carefully considering the consequences. In particular, it’s usually a bad idea to block on async code by calling Task.Wait or Task.Result.
There are two places I check whenever I have questions such as this:
The Can I Use website: http://caniuse.com/#search=await
And Node Green: http://node.green/#async-functions
Typically an answer is encouraged to include the relevant information to avoid link rot. But ironically this answer has exactly the opposite problem: this answer will rot (the information below will become invalid) long before the links above. So always check caniuse and node.green first:
From caniuse.com as of April 2019 :
From node.green as of April 2019
So depending on what you think is acceptable it is either safe or not safe. Note the following:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With