@putout/plugin-promises v18.0.0
@putout/plugin-promises 
A
Promiseis in one of these states:
- β
pending: initial state, neither fulfilled nor rejected;- β
fulfilled: meaning that the operation was completed successfully;- β
rejected: meaning that the operation failed;(c) MDN
πPutout plugin improves Promise-related code.
Install
npm i @putout/plugin-promises -DRules
- β add-missing-async;
- β add-missing-await;
- β apply-await-import;
- β apply-top-level-await;
- β apply-with-resolvers;
- β convert-new-promise-to-async;
- β convert-reject-to-throw;
- β remove-useless-async;
- β remove-useless-await;
- β remove-useless-resolve;
- β remove-useless-variables;
Config
{
"rules": {
"promises/add-missing-await": "on",
"promises/add-missing-async": "on",
"promises/apply-await-import": "on",
"promises/apply-top-level-await": "on",
"promises/apply-with-resolvers": "off",
"promises/remove-useless-resolve": "on",
"promises/remove-useless-async": "on",
"promises/remove-useless-await": "on",
"promises/remove-useless-variables": "on",
"promises/convert-reject-to-throw": "on",
"promises/convert-new-promise-to-async": "on"
}
}βοΈ If you want to override any of it, update .putout.json in the directory near your files.
π¦ Configuration section of πPutout documentation tell you more about all configuration options supported.
apply-await-import
add forgotten await to dynamic import().
β Example of incorrect code
const {readFile} = import('node:fs/promises');β Example of correct code
const {readFile} = await import('node:fs/promises');apply-with-resolvers
The
Promise.withResolvers()static method returns an object containing a newPromiseobject and two functions toresolveorrejectit, corresponding to the two parameters passed to the executor of thePromise()constructor.(c) MDN
Checkout in
- ππ±Mobile Putout Editor;
- πPutout Editor;
β Example of incorrect code
const promise = new Promise((res, rej) => {});β Example of correct code
const {
promise,
resolve,
reject,
} = Promise.withResolvers();remove-useless-resolve
β Example of incorrect code
async function hello() {
return Promise.resolve('hello');
}β Example of correct code
async function hello() {
return 'hello';
}remove-useless-async
β Example of incorrect code
async function hello() {
return 'hello';
}β Example of correct code
function hello() {
return 'hello';
}remove-useless-await
If a handler function returns another pending promise object, the resolution of the promise returned by
thenwill be subsequent to the resolution of the promise returned by the handler. Also, the resolved value of the promise returned bythenwill be the same as the resolved value of the promise returned by the handler.(c) MDN
β Example of incorrect code
await await Promise.resolve();
const hello = await 'world';β Example of correct code
await Promise.resolve();
const hello = 'world';convert-reject-to-throw
β Example of incorrect code
async function hello() {
return Promise.reject(Error('error'));
}β Example of correct code
async function hello() {
throw Error('error');
}add-missing-await
Using
return awaitinside anasync functionkeeps the currentfunctionin thecall stackuntil thePromisethat is being awaited has resolved, at the cost of an extra microtask before resolving the outerPromise.return awaitcan also be used in atry/catch statementto catch errors from another function that returns a Promise.You can avoid the extra microtask by not awaiting the return value, with the trade off of the function no longer being a part of the stack trace if an error is thrown asynchronously from the
Promisebeing returned. This can make debugging more difficult.(c) ESLint
β Example of incorrect code
runCli();
async function runCli() {}β Example of correct code
await runCli();
async function runCli() {}add-missing-async
The
asyncfunction declaration creates a binding of a new async function to a given name. Theawaitkeyword is permitted within the function body, enabling asynchronous, promise-based behavior to be written in a cleaner style and avoiding the need to explicitly configure promise chains.(c) MDN
β Example of incorrect code
function hello() {
await world();
}β Example of correct code
async function hello() {
await world();
}convert-new-promise-to-async
β Example of incorrect code
function get() {
return new Promise((resolve, reject) => {
reject(Error('Cannot get'));
});
}β Example of correct code
async function get() {
throw Error('Cannot get');
}apply-top-level-await
Applies top-level-await.
β Example of incorrect code
import {readFile} from 'node:fs/promises';
(async () => {
await readFile('./README.md', 'utf8');
})();β Example of correct code
import {readFile} from 'node:fs/promises';
await readFile('./README.md', 'utf8');remove-useless-variables
β Example of incorrect code
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
const result2 = await Promise.resolve(result);
return result2;
};β Example of correct code
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
return result;
};License
MIT
5 months ago
7 months ago
11 months ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago