waiter-promise
TypeScript icon, indicating that this package has built-in type declarations

1.3.4 • Public • Published

waiter-promise -- an enhanced promise library

Waiter is one of Deferred implementation, but with more helpful properties. We split Promise's resolve and reject method out of the declaration, know about settled status, and get result of resolve or reason of reject anytime.

install

npm i -S waiter-promise

# or with yarn
yarn add waiter-promise

use

Use in JavaScript Promise:

import Waiter from 'waiter-promise'

const waiterA = new Waiter()

console.log(waiterA.settled) // -> false
waiterA.then(() => {
  console.log(waiterA.result, waiterA.settled) // -> something result, true
})

const waiterB = new Waiter()
waiterB.catch(() => {
  console.log(waiterB.reason, waiterB.settled) // -> reject reason, true
})

waiterA.resolve('something result')
waiterB.reject('reject reason')

Use with async/await:

import Waiter from 'waiter-promise'

const waiter = new Waiter<string>()

const receive = async (): Promise<void> => {
  const res = await waiter.promise
  console.log(res)
}

const send = () => waiter.resolve('hello world')

send()
receive() // output: hello world

Readme

Keywords

Package Sidebar

Install

npm i waiter-promise

Weekly Downloads

2

Version

1.3.4

License

MIT

Unpacked Size

3.98 kB

Total Files

10

Last publish

Collaborators

  • letow