pax_global_header00006660000000000000000000000064140337622170014517gustar00rootroot0000000000000052 comment=0abf7f56337a450cbff45074dd8c83f8ace4726c p-defer-4.0.0/000077500000000000000000000000001403376221700130425ustar00rootroot00000000000000p-defer-4.0.0/.editorconfig000066400000000000000000000002571403376221700155230ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 p-defer-4.0.0/.gitattributes000066400000000000000000000000231403376221700157300ustar00rootroot00000000000000* text=auto eol=lf p-defer-4.0.0/.github/000077500000000000000000000000001403376221700144025ustar00rootroot00000000000000p-defer-4.0.0/.github/funding.yml000066400000000000000000000000531403376221700165550ustar00rootroot00000000000000github: sindresorhus tidelift: npm/p-defer p-defer-4.0.0/.github/security.md000066400000000000000000000002631403376221700165740ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. p-defer-4.0.0/.github/workflows/000077500000000000000000000000001403376221700164375ustar00rootroot00000000000000p-defer-4.0.0/.github/workflows/main.yml000066400000000000000000000006451403376221700201130ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test p-defer-4.0.0/.gitignore000066400000000000000000000000271403376221700150310ustar00rootroot00000000000000node_modules yarn.lock p-defer-4.0.0/.npmrc000066400000000000000000000000231403376221700141550ustar00rootroot00000000000000package-lock=false p-defer-4.0.0/index.d.ts000066400000000000000000000014501403376221700147430ustar00rootroot00000000000000export interface DeferredPromise { /** The deferred promise. */ promise: Promise; /** Resolves the promise with a value or the result of another promise. @param value - The value to resolve the promise with. */ resolve(value?: ValueType | PromiseLike): void; /** Reject the promise with a provided reason or error. @param reason - The reason or error to reject the promise with. */ reject(reason?: unknown): void; } /** Create a deferred promise. @example ``` import pDefer from 'p-defer'; function delay(milliseconds) { const deferred = pDefer(); setTimeout(deferred.resolve, milliseconds, '🦄'); return deferred.promise; } console.log(await delay(100)); //=> '🦄' ``` */ export default function pDefer(): DeferredPromise; p-defer-4.0.0/index.js000066400000000000000000000003051403376221700145050ustar00rootroot00000000000000export default function pDefer() { const deferred = {}; deferred.promise = new Promise((resolve, reject) => { deferred.resolve = resolve; deferred.reject = reject; }); return deferred; } p-defer-4.0.0/index.test-d.ts000066400000000000000000000005631403376221700157240ustar00rootroot00000000000000import {expectType} from 'tsd'; import pDefer, {DeferredPromise} from './index.js'; expectType>(pDefer()); expectType>(pDefer()); pDefer().resolve(); pDefer().resolve('foo'); pDefer().reject(); pDefer().reject(new Error('foo')); expectType>(pDefer().promise); p-defer-4.0.0/license000066400000000000000000000021351403376221700144100ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. p-defer-4.0.0/package.json000066400000000000000000000013121403376221700153250ustar00rootroot00000000000000{ "name": "p-defer", "version": "4.0.0", "description": "Create a deferred promise", "license": "MIT", "repository": "sindresorhus/p-defer", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "promise", "defer", "deferred", "resolve", "reject", "lazy", "later", "async", "await", "promises" ], "devDependencies": { "ava": "^3.15.0", "tsd": "^0.14.0", "xo": "^0.38.2" } } p-defer-4.0.0/readme.md000066400000000000000000000025331403376221700146240ustar00rootroot00000000000000# p-defer > Create a deferred promise [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. ## Install ``` $ npm install p-defer ``` ## Usage ```js import pDefer from 'p-defer'; function delay(milliseconds) { const deferred = pDefer(); setTimeout(deferred.resolve, milliseconds, '🦄'); return deferred.promise; } console.log(await delay(100)); //=> '🦄' ``` *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.* ## API ### pDefer() Returns an `object` with a `promise` property and functions to `resolve()` and `reject()`. ## Related - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called - [More…](https://github.com/sindresorhus/promise-fun) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
p-defer-4.0.0/test.js000066400000000000000000000004531403376221700143610ustar00rootroot00000000000000import test from 'ava'; import pDefer from './index.js'; const fixture = Symbol('fixture'); function delay(milliseconds) { const deferred = pDefer(); setTimeout(deferred.resolve, milliseconds, fixture); return deferred.promise; } test('main', async t => { t.is(await delay(50), fixture); });