Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use async/await now? [closed]

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?

like image 962
Abhijeet Avatar asked Oct 17 '22 12:10

Abhijeet


People also ask

When should I use async await?

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.

What can I use instead of async await?

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.

Is async await necessary?

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 .

Does async await stop execution?

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.

Is your async code safe?

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:

How do you know if Async Await is working?

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.

What is the difference between async and await in JavaScript?

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.

What does “async all the way” mean?

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.


Video Answer


1 Answers

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 :

  • IE *: NOT SUPPORTED (most used version = 11)
  • Edge: From version 15 (most used version = 17)
  • Firefox: From version 52 (most used version = 65)
  • Chrome: From version 55 (most used version = 72)
  • Safari: From version 10.1 (most used version = 12)
  • Opera: From version 42 (most used version = 58)
  • iOS Safari: From version 10.3 (most used version = 12.1)
  • Opera Mini *: NOT SUPPORTED
  • Android Browser *: From 5 (most used version = 4.4)
  • Chrome for Android: From version 55 (most used version = 71)

From node.green as of April 2019

  • Node.js: From version 8.0.0

So depending on what you think is acceptable it is either safe or not safe. Note the following:

  • This question was originally asked on 2017, and we have come a long way so async/await is much more safe to use now.
  • By 2019, most mobile devices already support async/await.
  • Node 8 is released on May 2017 so it should be safe to use async/await on Node.js unless your Node.js servers are very outdated.
like image 84
slebetman Avatar answered Oct 19 '22 02:10

slebetman