Skip to content

WangLeto/waiter-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Enhanced promise

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published