Create a deferred promise
Go to file
Derrick Hammer 534ba472a8
refactor: convert to cjs
2023-04-23 08:08:27 -04:00
.github Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
.editorconfig Require Node.js 6, add TypeScript definition (#2) 2019-03-17 19:21:07 +07:00
.gitattributes Require Node.js 6, add TypeScript definition (#2) 2019-03-17 19:21:07 +07:00
.gitignore Require Node.js 6, add TypeScript definition (#2) 2019-03-17 19:21:07 +07:00
.npmrc Require Node.js 6, add TypeScript definition (#2) 2019-03-17 19:21:07 +07:00
index.d.ts Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
index.js refactor: convert to cjs 2023-04-23 08:08:27 -04:00
index.test-d.ts Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
license Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
package.json refactor: convert to cjs 2023-04-23 08:08:27 -04:00
readme.md Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
test.js Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00

readme.md

p-defer

Create a deferred promise

Don't use this unless you know what you're doing. Prefer the Promise constructor.

Install

$ npm install p-defer

Usage

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 if you need to delay a promise.

API

pDefer()

Returns an object with a promise property and functions to resolve() and reject().

  • p-lazy - Create a lazy promise that defers execution until .then() or .catch() is called
  • More…

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.