πPutout plugin improves Promise-related code
npm install @putout/plugin-promises[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-promises.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-promises "npm"
> A Promise is 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.
```
npm i @putout/plugin-promises -D
- β
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;
`json`
{
"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.
add forgotten await to dynamic import().
`js`
const {readFile} = import('node:fs/promises');
`js`
const {readFile} = await import('node:fs/promises');
> The Promise.withResolvers() static method returns an object containing a new Promise object and two functions to resolve or reject it, corresponding to the two parameters passed to the executor of the Promise() constructor.
>
> (c) MDN
Checkout in
- ππ±Mobile Putout Editor;
- πPutout Editor;
`js`
const promise = new Promise((res, rej) => {});
`js`
const {
promise,
resolve,
reject,
} = Promise.withResolvers();
`js`
async function hello() {
return Promise.resolve('hello');
}
`js`
async function hello() {
return 'hello';
}
`js`
async function hello() {
return 'hello';
}
`js`
function hello() {
return 'hello';
}
> If a handler function returns another pending promise object, the resolution of the promise returned by then will be subsequent to the resolution of the promise returned by the handler. Also, the resolved value of the promise returned by then will be the same as the resolved value of the promise returned by the handler.
>
> (c) MDN
`js`
await await Promise.resolve();
const hello = await 'world';
`js`
await Promise.resolve();
const hello = 'world';
Checkout in πPutout Editor.
`js`
async function hello() {
return Promise.reject(Error('error'));
}
`js`
async function hello() {
throw Error('error');
}
> Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise. return await can also be used in a try/catch statement to 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 Promise being returned. This can make debugging more difficult.
>
> (c) ESLint
`js
runCli();
async function runCli() {}
`
`js
await runCli();
async function runCli() {}
`
> The async function declaration creates a binding of a new async function to a given name. The await keyword 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
`js`
function hello() {
await world();
}
`js`
async function hello() {
await world();
}
`js`
function get() {
return new Promise((resolve, reject) => {
reject(Error('Cannot get'));
});
}
`js`
async function get() {
throw Error('Cannot get');
}
Applies top-level-await.
`js
import {readFile} from 'node:fs/promises';
(async () => {
await readFile('./README.md', 'utf8');
})();
`
`js
import {readFile} from 'node:fs/promises';
await readFile('./README.md', 'utf8');
`
`js`
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
const result2 = await Promise.resolve(result);
return result2;
};
`js``
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
return result;
};
MIT