Skip to main content

no-misused-promises

Avoid using promises in places not designed to handle them.

This rule forbids using promises in places where the TypeScript compiler allows them but they are not handled properly. These situations can often arise due to a missing await keyword or just a misunderstanding of the way async functions are handled/awaited.

Rule Details

This rule accepts a single option which is an object with checksConditionals and checksVoidReturn properties indicating which types of misuse to flag. Both are enabled by default

Options

type Options = [
{
checksConditionals?: boolean;
checksVoidReturn?: boolean;
},
];

const defaultOptions: Options = [
{
checksConditionals: true,
checksVoidReturn: true,
},
];

If you don't want functions that return promises where a void return is expected to be checked, your configuration will look like this:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksVoidReturn": false
}
]
}

Likewise, if you don't want to check conditionals, you can configure the rule like this:

{
"@typescript-eslint/no-misused-promises": [
"error",
{
"checksConditionals": false
}
]
}

checksConditionals: true

Examples of code for this rule with checksConditionals: true:

const promise = Promise.resolve('value');

if (promise) {
// Do something
}

const val = promise ? 123 : 456;

while (promise) {
// Do something
}

checksVoidReturn: true

Examples of code for this rule with checksVoidReturn: true:

[1, 2, 3].forEach(async value => {
await doSomething(value);
});

new Promise(async (resolve, reject) => {
await doSomething();
resolve();
});

const eventEmitter = new EventEmitter();
eventEmitter.on('some-event', async () => {
synchronousCall();
await doSomething();
otherSynchronousCall();
});

When Not To Use It

If you do not use Promises in your codebase or are not concerned with possible misuses of them outside of what the TypeScript compiler will check.

Further Reading

Attributes

  • ✅ Recommended
  • 🔧 Fixable
  • 💭 Requires type information