Promises need to be resolved or awaited to return the expected value, otherwise, they return the promise object.
Unresolved promises:
Forgetting to await a promise is a frequent mistake. There are places where the use of a promise object is confusing or unclear because the
developer forgot to resolve it.
This rule forbids returning promises where another type is expected such as in:
- conditionals
- void returns
- spread operators
What is the potential impact?
Using a promise instead of its resolved value can have unexpected results leading to bugs.
- In conditionals, it will always return a truthy value.
- In places where the expected type is void, returning a promise is often a mistake.
- Using the spread operator on a promise will raise an exception.
The executor function of a promise can also be an async function. However, this usually denotes a mistake:
- If an async executor function throws an error, the error won’t cause the created promise to reject and will be lost. Therefore, this could make
it difficult to debug and handle runtime errors.
- If a promise executor function is using
await
, this means that it’s not necessary to use the Promise
constructor, or
the scope of the Promise constructor can be reduced.
Exceptions
This rule can be ignored for promises that you know will always resolve like timers.
await new Promise(resolve => time.setTimeout(1000));