This rule disallows awaiting a value that is not a Promise.
While it is valid JavaScript to await a non-Promise
-like value (it will resolve immediately), this pattern is often a programmer error, such as forgetting to add parenthesis to call a function that returns a Promise.
Examples of incorrect code for this rule:
await 'value';
const createValue = () => 'value';
await createValue();
// An array of Promises is not the same as an AsyncIterable
async function incorrect(arrayOfPromises: Array<Promise<string>>) {
for await (const element of arrayOfPromises) {
}
}
Examples of correct code for this rule:
await Promise.resolve('value');
const createValue = (async() = 'value');
await createValue();
async function overIterable(iterable: AsyncIterable<string>) {
for await (const element of iterable) {
}
}
async function overIterableIterator(iterable: AsyncIterableIterator<string>) {
for await (const element of iterable) {
}
}
The rule accepts an options object with the following property:
allowedPromiseNames
any extra names of classes or interfaces to be considered "awaitable" inawait
statements.
Classes named Promise
may always be awaited.
allowedPromiseNames
does not affect for-await-of
statements.
Examples of incorrect code for this rule with { allowedPromiseNames: ["Thenable"] }
:
class Thenable {
/* ... */
}
await new Thenable();
Examples of correct code for this rule with { allowedPromiseNames: ["Thenable"] }
:
class OtherClass {
/* ... */
}
await new OtherClass();
If you want to allow code to await
non-Promise values.
This is generally not preferred, but can sometimes be useful for visual consistency.
- TSLint: 'await-promise'