Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to warn when you forget to `await` an async function in Javascript?

I'm using Babel and Webpack. If I forget to await an async function, it can often go unnoticed. Once in a while, if I forgot the await, an error occurs in the async function and I get an Unhandled promise rejection. Then, I realize that I forgot the await.

Is there a way to get a warning when I forget to add an await?

like image 480
Leo Jiang Avatar asked Jan 16 '17 21:01

Leo Jiang


People also ask

What happens when you don't await an async function?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

What happens if you don't await async in JavaScript?

In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously. Code after each await expression can be thought of as existing in a . then callback.

What happens if you forget the await keyword in front of an asynchronous expression?

What if we forget the await keyword ? If you forget to use await while calling an async function, the function starts executing. This means that await is not required for executing the function. The async function will return a promise, which you can use later.

What happens when await fails JavaScript?

The await keyword before a promise makes JavaScript wait until that promise settles, and then: If it's an error, an exception is generated — same as if throw error were called at that very place. Otherwise, it returns the result.


1 Answers

I think OP is looking for something like no-floating-promises from tslint see: https://palantir.github.io/tslint/rules/no-floating-promises/

like image 166
ubershmekel Avatar answered Sep 21 '22 18:09

ubershmekel