The
Promise
object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.(c) MDN
🐊Putout plugin adds ability to work with promises.
npm i @putout/plugin-promises -D
{
"rules": {
"promises/add-missing-await": "on",
"promises/apply-await-import": "on",
"promises/apply-top-level-await": "on",
"promises/remove-useless-resolve": "on",
"promises/remove-useless-async": "on",
"promises/remove-useless-await": "on",
"promises/convert-reject-to-throw": "on",
"promises/convert-new-promise-to-async": "on"
}
}
add forgotten await to dynamic import()
.
const {readFile} = import('fs/promises');
const {readFile} = await import('fs/promises');
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');
}
runCli();
async function runCli() {
}
await runCli();
async function runCli() {
}
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 'fs/promises';
(async () => {
await readFile('./README.md', 'utf8');
})();
import {readFile} from 'fs/promises';
await readFile('./README.md', 'utf8');
MIT