unicorn/no-await-expression-member
Disallow reading a property directly from an await expression
fixable
(await getUser(id)).name needs parentheses, and they're easy to forget. Without them, await getUser(id).name reads name from the promise and gives you undefined. If you destructure, or put the awaited value in a variable, you don't need parentheses and the code reads from left to right.
--fix-dangerously rewrites const name = (await getUser(id)).name and const first = (await getPosts(id))[0] to destructuring. You need to rewrite other forms by hand.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-await-expression-member" = "error" # or "warn"Examples#
example.js2 problems
async function show(id) {
const name = (await getUser(id)).nameDo not access a member directly from an await expression.;
const first = (await getPosts(id))[0]Do not access a member directly from an await expression.;
render(name, first);
}example.js1 problem
async function load(url) {
return (await fetch(url)).jsonDo not access a member directly from an await expression.();
}example.jsNo problems
async function show(id) {
const { name } = await getUser(id);
const [first] = await getPosts(id);
render(name, first);
}example.jsNo problems
async function load(url) {
const response = await fetch(url);
return response.json();
}