{"name":"@putout/plugin-promises","version":"17.0.0","keywords":["putout","putout-plugin","plugin","esm","commonjs"],"author":{"url":"https://github.com/coderaiser","name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"license":"MIT","_id":"@putout/plugin-promises@17.0.0","maintainers":[{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"}],"homepage":"https://github.com/coderaiser/putout/tree/master/packages/plugin-promises#readme","bugs":{"url":"https://github.com/coderaiser/putout/issues"},"tag":false,"dist":{"shasum":"1d35f151eac9c4a647887bcc93f7ca5dde1c47c0","tarball":"http://registry-npm.ywwl.com/@putout/plugin-promises/download/@putout/plugin-promises-17.0.0.tgz","fileCount":15,"integrity":"sha512-/Dpxs7ie20rfusLtCtbqPYUvpUD++Y9msI+g0g07eDll7pVP4R0Ufty03oAetTNMMFiqcowbRpYs53EJ6xVncw==","signatures":[{"sig":"MEYCIQCgIA2qE2Y7GAjHZ862OxFQJPfWu02K2NblRogFD29jFQIhAOXVjsPDvH9UAd3ujnPWytrLuVcrprVt3vW6wzU4WJXD","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":23312,"size":6456,"noattachment":false,"key":"/@putout/plugin-promises/-/@putout/plugin-promises-17.0.0.tgz"},"main":"lib/index.js","type":"module","engines":{"node":">=20"},"gitHead":"4a0a85aa0c8a829e6f5c1555188934f253735f83","release":false,"scripts":{"lint":"madrun lint","test":"madrun test","report":"madrun report","coverage":"madrun coverage","fix:lint":"madrun fix:lint","fresh:lint":"madrun fresh:lint","lint:fresh":"madrun lint:fresh","watch:test":"madrun watch:test"},"_npmUser":{"name":"coderaiser","email":"mnemonic.enemy@gmail.com"},"changelog":false,"repository":{"url":"git+https://github.com/coderaiser/putout.git","type":"git"},"_npmVersion":"11.1.0","description":"🐊Putout plugin improves Promise-related code","directories":{},"_nodeVersion":"22.14.0","dependencies":{"fullstore":"^3.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","eslint":"^9.0.0","madrun":"^11.0.0","nodemon":"^3.0.1","@putout/test":"^13.0.0","eslint-plugin-n":"^17.0.0","eslint-plugin-putout":"^26.0.0"},"peerDependencies":{"putout":">=39"},"_npmOperationalInternal":{"tmp":"tmp/plugin-promises_17.0.0_1743081477581_0.32788500356470074","host":"s3://npm-registry-packages-npm-production"},"readme":"# @putout/plugin-promises [![NPM version][NPMIMGURL]][NPMURL]\n\n[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-promises.svg?style=flat\\&longCache=true\n[NPMURL]: https://npmjs.org/package/@putout/plugin-promises \"npm\"\n\n> A `Promise` is in one of these states:\n>\n> - ✅ `pending`: initial state, neither fulfilled nor rejected;\n> - ✅ `fulfilled`: meaning that the operation was completed successfully;\n> - ✅ `rejected`: meaning that the operation failed;\n>\n> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n\n🐊[**Putout**](https://github.com/coderaiser/putout) plugin improves `Promise`-related code.\n\n## Install\n\n```\nnpm i @putout/plugin-promises -D\n```\n\n## Rules\n\n- ✅ [add-missing-async](#add-missing-async);\n- ✅ [add-missing-await](#add-missing-await);\n- ✅ [apply-await-import](#apply-await-import);\n- ✅ [apply-top-level-await](#apply-top-level-await);\n- ✅ [apply-with-resolvers](#apply-with-resolvers);\n- ✅ [convert-new-promise-to-async](#convert-new-promise-to-async);\n- ✅ [convert-reject-to-throw](#convert-reject-to-throw);\n- ✅ [convert-resolve-to-async](#convert-resolve-to-async);\n- ✅ [remove-useless-async](#remove-useless-async);\n- ✅ [remove-useless-await](#remove-useless-await);\n- ✅ [remove-useless-resolve](#remove-useless-resolve);\n- ✅ [remove-useless-variables](#remove-useless-variables);\n\n## Config\n\n```json\n{\n    \"rules\": {\n        \"promises/add-missing-async\": \"on\",\n        \"promises/add-missing-await\": \"on\",\n        \n        \"promises/apply-await-import\": \"on\",\n        \"promises/apply-top-level-await\": \"on\",\n        \"promises/apply-with-resolvers\": \"off\",\n        \n        \"promises/convert-new-promise-to-async\": \"on\",\n        \"promises/convert-reject-to-throw\": \"on\",\n        \"promises/convert-resolve-to-async\": \"on\",\n        \"promises/remove-useless-async\": \"on\",\n        \"promises/remove-useless-await\": \"on\",\n        \"promises/remove-useless-resolve\": \"on\",\n        \n        \"promises/remove-useless-variables\": \"on\"\n    }\n}\n```\n\n☝️ If you want to override any of it, update `.putout.json` in the directory near your files.\n\n[🦉 Configuration](https://github.com/coderaiser/putout#-configuration) section of 🐊**Putout** documentation tell you more about all configuration options supported.\n\n## add-missing-async\n\n> 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.\n>\n> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)\n\n### ❌ Example of incorrect code\n\n```js\nfunction hello() {\n    await world();\n}\n```\n\n### ✅ Example of correct code\n\n```js\nasync function hello() {\n    await world();\n}\n```\n\n## add-missing-await\n\n> 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.\n\n> 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.\n>\n> (c) [ESLint](eslint.org/docs/latest/rules/no-return-await)\n\n### ❌ Example of incorrect code\n\n```js\nrunCli();\n\nasync function runCli() {}\n```\n\n### ✅ Example of correct code\n\n```js\nawait runCli();\n\nasync function runCli() {}\n```\n\n## apply-await-import\n\nadd forgotten **await** to [**dynamic `import()`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports).\n\n### ❌ Example of incorrect code\n\n```js\nconst {readFile} = import('node:fs/promises');\n```\n\n### ✅ Example of correct code\n\n```js\nconst {readFile} = await import('node:fs/promises');\n```\n\n## apply-top-level-await\n\nApplies [top-level-await](https://v8.dev/features/top-level-await).\n\n### ❌ Example of incorrect code\n\n```js\nimport {readFile} from 'node:fs/promises';\n\n(async () => {\n    await readFile('./README.md', 'utf8');\n})();\n```\n\n### ✅ Example of correct code\n\n```js\nimport {readFile} from 'node:fs/promises';\n\nawait readFile('./README.md', 'utf8');\n```\n\n## apply-with-resolvers\n\n> 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.\n>\n> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers)\n\nCheckout in\n\n- 🐊📱[**Mobile Putout Editor**](https://putout.vercel.app/#/gist/e93dc4b71d05661e5d96141f31880945/de8940e2b8ecaebfcfe782cf25b221cb48830a03);\n- 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e93dc4b71d05661e5d96141f31880945/de8940e2b8ecaebfcfe782cf25b221cb48830a03);\n\n### ❌ Example of incorrect code\n\n```js\nconst promise = new Promise((res, rej) => {});\n```\n\n### ✅ Example of correct code\n\n```js\nconst {\n    promise,\n    resolve,\n    reject,\n} = Promise.withResolvers();\n```\n\n## convert-new-promise-to-async\n\n### ❌ Example of incorrect code\n\n```js\nfunction get() {\n    return new Promise((resolve, reject) => {\n        reject(Error('Cannot get'));\n    });\n}\n```\n\n### ✅ Example of correct code\n\n```js\nasync function get() {\n    throw Error('Cannot get');\n}\n```\n\n## convert-reject-to-throw\n\nCheckout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/713b90dbccb014eca7c23c9b2756c31d/3ce19b35a409a455fe1b3cf7bbf8cbed1b594512).\n\n### ❌ Example of incorrect code\n\n```js\nasync function hello() {\n    return Promise.reject(Error('error'));\n}\n```\n\n### ✅ Example of correct code\n\n```js\nasync function hello() {\n    throw Error('error');\n}\n```\n\n## convert-resolve-to-async\n\n> The **async function** declaration creates a binding of a new async function to a given name.\n>\n> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)\n\nCheckout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/67e173a6814de807d3ac7fd0276242a0/402665096e866a7b39bd25feb4af5f8621917105).\n\n### ❌ Example of incorrect code\n\n```js\nx.stub(() => Promise.resolve({\n    hello: 'world',\n}));\n```\n\n### ✅ Example of correct code\n\n```js\nx.stub(async () => ({\n    hello: 'world',\n}));\n```\n\n## remove-useless-async\n\n### ❌ Example of incorrect code\n\n```js\nasync function hello() {\n    return 'hello';\n}\n```\n\n### ✅ Example of correct code\n\n```js\nfunction hello() {\n    return 'hello';\n}\n```\n\n## remove-useless-await\n\n> 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.\n>\n> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#return_value)\n\n### ❌ Example of incorrect code\n\n```js\nawait await Promise.resolve();\nconst hello = await 'world';\n```\n\n### ✅ Example of correct code\n\n```js\nawait Promise.resolve();\nconst hello = 'world';\n```\n\n## remove-useless-resolve\n\n### ❌ Example of incorrect code\n\n```js\nasync function hello() {\n    return Promise.resolve('hello');\n}\n```\n\n### ✅ Example of correct code\n\n```js\nasync function hello() {\n    return 'hello';\n}\n```\n\n## remove-useless-variables\n\n### ❌ Example of incorrect code\n\n```js\nasync () => {\n    const result = transformer.transform(realTransformer, transformCode, code, parser);\n    \n    const result2 = await Promise.resolve(result);\n    \n    return result2;\n};\n```\n\n### ✅ Example of correct code\n\n```js\nasync () => {\n    const result = transformer.transform(realTransformer, transformCode, code, parser);\n    \n    return result;\n};\n```\n\n## License\n\nMIT\n","publish_time":1743081477777,"_cnpm_publish_time":1743081477777,"dist-tags":{"latest":"20.2.0"}}