Skip to content

garmjs/delay

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

delay Build Status

Delay a promise a specified amount of time

Install

$ npm install --save delay

Usage

const delay = require('delay');

delay(200)
	.then(() => {
		// executed after 200 milliseconds
	});

somePromise()
	.then(delay(100))
	.then(result => {
		// executed 100 milliseconds after somePromise resolves
		// the result from somePromise is passed through
	});

// and with Babel and async functions
async () => {
	bar();

	await delay(100);

	// executed 100 milliseconds later
	baz();
}();

// there's also delay.reject() that takes the value, and rejects it `ms` later
Promise.resolve('foo')
	.then(delay.reject(100))
	.then(x => blah()) // never executed
	.catch(err => {
		// executed 100 milliseconds later
		// err === 'foo'
	});

// you can also specify the rejection value
Promise.resolve('foo')
	.then(delay.reject(100, 'bar'))
	.then(x => blah()) // never executed
	.catch(err => {
		// executed 100 milliseconds later
		// err === 'bar'
	});

Related

  • p-immediate - Returns a promise resolved in the next event loop - think setImmediate()
  • p-timeout - Timeout a promise after a specified amount of time
  • More…

License

MIT © Sindre Sorhus

About

Delay a promise a specified amount of time

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%