@putout/plugin-promises
putout plugin for promises processing
Last updated 4 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-promises 
SYNC missed versions from official npm registry.

@putout/plugin-promises NPM version

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.

Install

npm i @putout/plugin-promises -D

Rules

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 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

❌ 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 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

❌ 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 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

❌ Example of incorrect code

runCli();

async function runCli() {}

✅ Example of correct code

await runCli();

async function runCli() {}

add-missing-async

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

❌ 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

Current Tags

  • 15.2.0                                ...           latest (6 months ago)

45 Versions

  • 15.2.0                                ...           6 months ago
  • 15.1.0                                ...           6 months ago
  • 15.0.0                                ...           8 months ago
  • 14.1.0                                ...           a year ago
  • 14.0.0                                ...           a year ago
  • 13.1.0                                ...           a year ago
  • 13.0.0                                ...           a year ago
  • 12.0.0                                ...           a year ago
  • 11.9.0                                ...           a year ago
  • 11.8.0                                ...           a year ago
  • 11.7.0                                ...           a year ago
  • 11.6.0                                ...           a year ago
  • 11.5.1                                ...           a year ago
  • 11.5.0                                ...           a year ago
  • 11.4.0                                ...           a year ago
  • 11.3.0                                ...           a year ago
  • 11.2.0                                ...           a year ago
  • 11.1.0                                ...           a year ago
  • 11.0.0                                ...           a year ago
  • 10.0.0                                ...           2 years ago
  • 9.1.0                                ...           2 years ago
  • 9.0.0                                ...           2 years ago
  • 8.4.0                                ...           3 years ago
  • 8.3.0                                ...           3 years ago
  • 8.2.0                                ...           3 years ago
  • 8.1.0                                ...           3 years ago
  • 8.0.0                                ...           3 years ago
  • 7.0.0                                ...           3 years ago
  • 6.2.1                                ...           3 years ago
  • 6.2.0                                ...           3 years ago
  • 6.1.0                                ...           3 years ago
  • 6.0.0                                ...           3 years ago
  • 5.0.0                                ...           4 years ago
  • 4.8.0                                ...           4 years ago
  • 4.7.0                                ...           4 years ago
  • 4.6.0                                ...           4 years ago
  • 4.5.0                                ...           4 years ago
  • 4.4.0                                ...           4 years ago
  • 4.3.0                                ...           4 years ago
  • 4.2.0                                ...           4 years ago
  • 4.1.0                                ...           4 years ago
  • 4.0.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (8)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE