├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface DeferredPromise { 2 | /** 3 | The deferred promise. 4 | */ 5 | promise: Promise; 6 | 7 | /** 8 | Resolves the promise with a value or the result of another promise. 9 | 10 | @param value - The value to resolve the promise with. 11 | */ 12 | resolve(this: void, value?: ValueType | PromiseLike): void; 13 | 14 | /** 15 | Reject the promise with a provided reason or error. 16 | 17 | @param reason - The reason or error to reject the promise with. 18 | */ 19 | reject(this: void, reason?: unknown): void; 20 | } 21 | 22 | /** 23 | Create a deferred promise. 24 | 25 | @example 26 | ``` 27 | import pDefer from 'p-defer'; 28 | 29 | function delay(milliseconds) { 30 | const deferred = pDefer(); 31 | setTimeout(deferred.resolve, milliseconds, '🦄'); 32 | return deferred.promise; 33 | } 34 | 35 | console.log(await delay(100)); 36 | //=> '🦄' 37 | ``` 38 | */ 39 | export default function pDefer(): DeferredPromise; 40 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function pDefer() { 2 | const deferred = {}; 3 | 4 | deferred.promise = new Promise((resolve, reject) => { 5 | deferred.resolve = resolve; 6 | deferred.reject = reject; 7 | }); 8 | 9 | return deferred; 10 | } 11 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import pDefer, {DeferredPromise} from './index.js'; 3 | 4 | expectType>(pDefer()); 5 | expectType>(pDefer()); 6 | 7 | pDefer().resolve(); 8 | pDefer().resolve('foo'); 9 | pDefer().reject(); 10 | pDefer().reject(new Error('foo')); 11 | expectType>(pDefer().promise); 12 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-defer", 3 | "version": "4.0.1", 4 | "description": "Create a deferred promise", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-defer", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": ">=12" 19 | }, 20 | "scripts": { 21 | "test": "ava" 22 | }, 23 | "files": [ 24 | "index.js", 25 | "index.d.ts" 26 | ], 27 | "keywords": [ 28 | "promise", 29 | "defer", 30 | "deferred", 31 | "resolve", 32 | "reject", 33 | "lazy", 34 | "later", 35 | "async", 36 | "await", 37 | "promises" 38 | ], 39 | "devDependencies": { 40 | "ava": "^3.15.0", 41 | "tsd": "^0.14.0", 42 | "xo": "^0.38.2" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-defer 2 | 3 | > Create a deferred promise 4 | 5 | [Don't use this unless you know what you're doing.](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern) Prefer the `Promise` constructor. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install p-defer 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import pDefer from 'p-defer'; 17 | 18 | function delay(milliseconds) { 19 | const deferred = pDefer(); 20 | setTimeout(deferred.resolve, milliseconds, '🦄'); 21 | return deferred.promise; 22 | } 23 | 24 | console.log(await delay(100)); 25 | //=> '🦄' 26 | ``` 27 | 28 | *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.* 29 | 30 | ## API 31 | 32 | ### pDefer() 33 | 34 | Returns an `object` with a `promise` property and functions to `resolve()` and `reject()`. 35 | 36 | ## Related 37 | 38 | - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called 39 | - [More…](https://github.com/sindresorhus/promise-fun) 40 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import pDefer from './index.js'; 3 | 4 | const fixture = Symbol('fixture'); 5 | 6 | function delay(milliseconds) { 7 | const deferred = pDefer(); 8 | setTimeout(deferred.resolve, milliseconds, fixture); 9 | return deferred.promise; 10 | } 11 | 12 | test('main', async t => { 13 | t.is(await delay(50), fixture); 14 | }); 15 | --------------------------------------------------------------------------------