npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@putout/plugin-promises

v15.0.0

Published

🐊Putout plugin improves Promise-related code

Downloads

68,081

Readme

@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/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/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');

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