Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the
void
operator53 for (const [key, statement] of Object.entries(PRINT_COMMANDS)) {
54 context.subscriptions.push(
55 vscode.commands.registerCommand(statement.command, () => {
56 executeCommand(statement.statement);57 })
58 );
59 }
Description
Usage of Promise-like
values in statements without handling their errors appropriately is dangerous.
Unhandled promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections and more.
Valid ways of handling a Promise-valued statement include awaiting
, returning
, and either calling .then()
with two arguments or .catch()
with one argument.
If you'd like to ignore unhandled promises at their call site, consider using the void
operator:
void returnsPromise();
Bad Practice
const promise = new Promise((resolve, reject) => resolve('value'));
promise;
async function returnsPromise() {
return 'value';
}
returnsPromise().then(() => {});
Promise.reject('value').catch();
Promise.reject('value').finally();
Recommended
const promise = new Promise((resolve, reject) => resolve('value'));
await promise;
async function returnsPromise() {
return 'value';
}
returnsPromise().then(
() => {},
() => {},
);
Promise.reject('value').catch(() => {});
Promise.reject('value').finally(() => {});