async-defer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Async defer

This package provide 2 async utilities

Defer

A resolvable/rejectable promise from outside is constructor.

/!\ The defer concept is not included in Promise specification for good reasons. Do not use this abusively!

Usage

const defer = new Defer<string>();
const value = await defer.promise;

defer.resolve('blah');

console.log(value); // > "blah"
const defer = new Defer<string>();
try {
  const value = await defer.promise;
  defer.reject('blah');
} catch (error) {
  console.log(error); // > "blah"
}

Async lock

Usage

const asyncLock = new AsyncLock();
asyncLock.lock('example');

setTimeout(() => {
  asyncLock.unlock('example');
}, 1000);

await asyncLock.pending;
console.log('This log will be printed after 1s');

Readme

Keywords

none

Package Sidebar

Install

npm i async-defer

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

20.8 kB

Total Files

15

Last publish

Collaborators

  • kepennar