unicorn/prefer-await
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-await": "error",
},
},
];async function run(promise) {
return promise.then(value => {
return transform(value);
});
}promise.catch(handleError);promise.finally(cleanup);async function run(promise) {
const value = await promise;
return transform(value);
}(async () => {
const value = await promise;
alert(value);
})();// Use an async IIFE when the surrounding scope cannot use `await` and the work should remain fire-and-forget.
void (async () => {
try {
await promise;
} catch (error) {
await handleError(error);
}
})();Examples from the ESLint documentation.
unicorn/prefer-await
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-await": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer last-oriented array methods over Array#reverse() or Array#toReversed() followed by a method.
Prefer direct iteration over default iterator method calls.
Prefer Array.from({length}, …) when creating range arrays.
Prefer .replaceChildren() when replacing DOM children.
Prefer using the Array.from() mapping function argument.
Prefer early returns over conditionals wrapping the remainder of the function body.