|
| 1 | +# Avoid using promises in places not designed to handle them (no-misused-promises) |
| 2 | + |
| 3 | +This rule forbids using promises in places where the Typescript compiler |
| 4 | +allows them but they are not handled properly. These situations can often arise |
| 5 | +due to a missing `await` keyword or just a misunderstanding of the way async |
| 6 | +functions are handled/awaited. |
| 7 | + |
| 8 | +## Rule Details |
| 9 | + |
| 10 | +Examples of **incorrect** code for this rule with `checksConditionals: true`: |
| 11 | + |
| 12 | +```ts |
| 13 | +const promise = Promise.resolve('value'); |
| 14 | + |
| 15 | +if (promise) { |
| 16 | + // Do something |
| 17 | +} |
| 18 | + |
| 19 | +const val = promise ? 123 : 456; |
| 20 | + |
| 21 | +while (promise) { |
| 22 | + // Do something |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Examples of **incorrect** code for this rule with `checksVoidReturn: true`: |
| 27 | + |
| 28 | +```ts |
| 29 | +[1, 2, 3].forEach(async value => { |
| 30 | + await doSomething(value); |
| 31 | +}); |
| 32 | + |
| 33 | +new Promise(async (resolve, reject) => { |
| 34 | + await doSomething(); |
| 35 | + resolve(); |
| 36 | +}); |
| 37 | + |
| 38 | +const eventEmitter = new EventEmitter(); |
| 39 | +eventEmitter.on('some-event', async () => { |
| 40 | + await doSomething(); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +Examples of **correct** code for this rule: |
| 45 | + |
| 46 | +```ts |
| 47 | +const promise = Promise.resolve('value'); |
| 48 | + |
| 49 | +if (await promise) { |
| 50 | + // Do something |
| 51 | +} |
| 52 | + |
| 53 | +const val = (await promise) ? 123 : 456; |
| 54 | + |
| 55 | +while (await promise) { |
| 56 | + // Do something |
| 57 | +} |
| 58 | + |
| 59 | +for (const value of [1, 2, 3]) { |
| 60 | + await doSomething(value); |
| 61 | +} |
| 62 | + |
| 63 | +new Promise((resolve, reject) => { |
| 64 | + // Do something |
| 65 | + resolve(); |
| 66 | +}); |
| 67 | + |
| 68 | +const eventEmitter = new EventEmitter(); |
| 69 | +eventEmitter.on('some-event', () => { |
| 70 | + doSomething(); |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +## Options |
| 75 | + |
| 76 | +This rule accepts a single option which is an object with `checksConditionals` |
| 77 | +and `checksVoidReturn` properties indicating which types of misuse to flag. |
| 78 | +Both are enabled by default |
| 79 | + |
| 80 | +If you don't want functions that return promises where a void return is |
| 81 | +expected to be checked, your configuration will look like this: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "@typescript-eslint/no-misused-promises": [ |
| 86 | + "error", |
| 87 | + { |
| 88 | + "checksVoidReturn": false |
| 89 | + } |
| 90 | + ] |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Likewise, if you don't want to check conditionals, you can configure the rule |
| 95 | +like this: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "@typescript-eslint/no-misused-promises": [ |
| 100 | + "error", |
| 101 | + { |
| 102 | + "checksConditionals": false |
| 103 | + } |
| 104 | + ] |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## When Not To Use It |
| 109 | + |
| 110 | +If you do not use Promises in your codebase or are not concerned with possible |
| 111 | +misuses of them outside of what the Typescript compiler will check. |
| 112 | + |
| 113 | +## Related to |
| 114 | + |
| 115 | +- [`no-floating-promises`]('./no-floating-promises.md') |
| 116 | + |
| 117 | +## Further Reading |
| 118 | + |
| 119 | +- [Typescript void function assignability](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void) |
0 commit comments