$ npm install @putout/plugin-promises
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
{
"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()
.
const {readFile} = import('node:fs/promises');
const {readFile} = await import('node:fs/promises');
The
Promise.withResolvers()
static method returns an object containing a newPromise
object and two functions toresolve
orreject
it, corresponding to the two parameters passed to the executor of thePromise()
constructor.(c) MDN
Checkout in
const promise = new Promise((res, rej) => {});
const {
promise,
resolve,
reject,
} = Promise.withResolvers();
async function hello() {
return Promise.resolve('hello');
}
async function hello() {
return 'hello';
}
async function hello() {
return 'hello';
}
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 bythen
will be the same as the resolved value of the promise returned by the handler.(c) MDN
await await Promise.resolve();
const hello = await 'world';
await Promise.resolve();
const hello = 'world';
async function hello() {
return Promise.reject(Error('error'));
}
async function hello() {
throw Error('error');
}
Using
return await
inside anasync function
keeps the currentfunction
in thecall stack
until thePromise
that is being awaited has resolved, at the cost of an extra microtask before resolving the outerPromise
.return await
can also be used in atry/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
runCli();
async function runCli() {}
await runCli();
async function runCli() {}
The
async
function declaration creates a binding of a new async function to a given name. Theawait
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
function hello() {
await world();
}
async function hello() {
await world();
}
function get() {
return new Promise((resolve, reject) => {
reject(Error('Cannot get'));
});
}
async function get() {
throw Error('Cannot get');
}
Applies top-level-await.
import {readFile} from 'node:fs/promises';
(async () => {
await readFile('./README.md', 'utf8');
})();
import {readFile} from 'node:fs/promises';
await readFile('./README.md', 'utf8');
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
const result2 = await Promise.resolve(result);
return result2;
};
async () => {
const result = transformer.transform(realTransformer, transformCode, code, parser);
return result;
};
MIT
© 2010 - cnpmjs.org x YWFE | Home | YWFE