How to warn when you forget to `await` an async function in Javascript?
I think OP is looking for something like no-floating-promises
from tslint
see: https://palantir.github.io/tslint/rules/no-floating-promises/
How to warn when you forget to `await` an async function in Javascript?
I think OP is looking for something like no-floating-promises
from tslint
see: https://palantir.github.io/tslint/rules/no-floating-promises/
When does the async / await chaining stop?
Call the asynchronous function once, at the beginning of your script. After its response comes back, call the rest of your script with the result synchronously. Something along the lines of:
async function functionC() {
//here is the real async call
const response = await fetch(....);
const result = await result.json();
return result;
}
functionC()
.then((results) => {
// do stuff with results
functionA(results);
functionB(results); // if needed
// etc
})
.catch(handleErrors); // don't forget this part
This way, only one part of your script - near the entry point - needs to handle the asynchronous logic. Implementing this may well require some refactoring, but it's worth it over the alternative of making everything async.
(also note that .json
resolves to a plain object or array, probably - it doesn't resolve to a string in JSON format, it parses the Response stream as if it was JSON and resolves to the result, which is no longer a JSON-formatted string, but a plain value, usually an object or array)
Async / await vs then which is the best for performance?
From a performance point of view, await
is just an internal version of .then()
(doing basically the same thing). The reason to choose one over the other doesn't really have to do with performance, but has to do with desired coding style or coding convenience. Certainly, the interpreter has a few more opportunities to optimize things internally with await
, but its unlikely that should be how you decide which to use. If all else was equal, I would choose await
for the reason cited above. But, I'd first choose which made the code simpler to write and understand and maintain and test.
Used properly, await
can often save you a bunch of lines of code making your code simpler to read, test and maintain. That's why it was invented.
There's no meaningful difference between the two versions of your code. Both achieve the same result when the axios call is successful or has an error.
Where await
could make more of a convenience difference is if you had multiple successive asynchronous calls that needed to be serialized. Then, rather than bracketing them each inside a .then()
handler to chain them properly, you could just use await
and have simpler looking code.
A common mistake with both await
and .then()
is to forget proper error handling. If your error handling desire in this function is to just return the rejected promise, then both of your versions do that identically. But, if you have multiple async calls in a row and you want to do anything more complex than just returning the first rejection, then the error handling techniques for await
and .then()
/.catch()
are quite different and which seems simpler will depend upon the situation.
Related Topics
What Is the Meaning of an Underscore in JavaScript Function Parameter
Can't Trigger Click with Jquery in a Chrome Extension
How to Remove an Object from an Array with JavaScript
Passing in Dynamic Key:Value Pairs to an Object Literal
Random Number Generator Without Dupes in JavaScript
JavaScript - How to Make This Code Work
How to Verify Jquery Ajax Events with Jasmine
Jest: Timer and Promise Don't Work Well. (Settimeout and Async Function)
Getting How Many People Are in a Chat Room in Socket.Io
How to Access Object Property with Invalid Characters
Get Selected Text Position and Place an Element Next to It
Node-Style Require for In-Browser JavaScript
How to Detect Responsive Breakpoints of Twitter Bootstrap 3 Using JavaScript