@putout/plugin-promises

putout plugin for promises processing

Usage no npm install needed!

<script type="module">
  import putoutPluginPromises from 'https://cdn.skypack.dev/@putout/plugin-promises';
</script>

README

@putout/plugin-promises NPM version

🐊Putout plugin adds ability to work with promises.

Install

npm i @putout/plugin-promises -D

Rule

{
    "rules": {
        "promises/add-missing-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",
        "promises/apply-top-level-await": "on"
    }
}

Add-return-await

❌ Incorrect

async function hello() {
    return world();
}

async function world() {
}

✅ Correct

async function hello() {
    return await world();
}

async function world() {
}

remove-useless-resolve

❌ Incorrect

async function hello() {
    return Promise.resolve('hello');
}

✅ Correct

async function hello() {
    return 'hello';
}

remove-useless-async

❌ Incorrect

async function hello() {
    return 'hello';
}

✅ Correct

function hello() {
    return 'hello';
}

remove-useless-await

❌ Incorrect

await await Promise.resolve();

✅ Correct

await await Promise.resolve();

convert-reject-to-throw

❌ Incorrect

async function hello() {
    return Promise.reject(Error('error'));
}

✅ Correct

async function hello() {
    throw Error('error');
}

add-missing-await

❌ Incorrect

runCli();

async function runCli() {
}

✅ Correct

await runCli();

async function runCli() {
}

convert-new-promise-to-async

❌ Incorrect

function get() {
    return new Promise((resolve, reject) => {
        reject(Error("Cannot get"));
    });
}

✅ Correct

async function get() {
    throw Error("Cannot get");
}

apply-top-level-await

Applies top-level-await.

❌ Incorrect

import {readFile} from 'fs/promises';

(async () => {
    await readFile('./README.md', 'utf8');
})();

✅ Correct

import {readFile} from 'fs/promises';

await readFile('./README.md', 'utf8');

License

MIT