Skip to content

tranvansang/jdefer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Javascript promise defer library

Install

yarn add jdefer

Usage

Single promise with defer

In 2024: recommended using Promise.withResolvers().

import makeDefer from 'jdefer'
// or
const makeDefer = require('jdefer')

const defer = makeDefer()

// to reject
defer.reject(new Error('foo'))

// to resolve
defer.resolve('bar')

// to wait
await defer.promise

Broadcast stream

import {makeBroadcastStream} from 'jdefer'
// or
const {makeBroadcastStream} = require('jdefer')

const stream = makeBroadcastStream()

// to listen
const removeListener = stream.listen(
	(value) => {
		console.log(value)
	}, {
		onDone() {
			console.log('done')
		},
		onError(error) {
			console.error(error)
		},
	})

// to stop listening
removeListener()

// to broadcast
stream.next('foo')
// to broadcast error
stream.throw(new Error('bar'))
// to broadcast done
stream.done()
// iterate
for await (const value of stream) {
	console.log(value)
}

About

Javascript promise defer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published