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

2.0.2 • Public • Published

Inside Out Async

Release

It's pretty handy for turning things that are not promises and generators into promises and generators. Good for testing, but also for having more control over how things execute.

npm install inside-out-async

I nerd sniped myself and made this library. defer() is easily written in any new project, deferGenerator() is not.

Exports two functions

API

withResolvers

function withResolvers<T>(): Deferred<T>

interface Deferred<T> {
    promise: Promise<T>
    resolve: (value: T) => void
    reject: (error: Error) => void
}

Returns an object containing a new Promise object and two functions to resolve or reject it, corresponding to the two parameters passed to the executor of the Promise() constructor. Like the Promise constructor but inside out.

Update 2.0.0 - With the release of Promise.withResolvers we've renamed defer to withResolvers. It is a suitable polyfill until browser support and user upgrades reach your particular critical mass.

import { withResolvers } from 'inside-out-async'
import { Trainer } from 'pokemon-trainer'

const { resolve, reject, promise } = withResolvers() // exactly the same as Promise.withResolvers()
const ash = new Trainer()
ash.on('capture', resolve)
ash.on('error', reject)

const pokemon = await promise
console.log(pokemon) // { name: 'Pikachu', temperament: 'surprised' }

deferGenerator

function deferGenerator<T, TReturn, TNext = unknown>(): DeferredGenerator<T, TReturn, TNext>

interface DeferredGenerator<T, TReturn, TNext> {
    generator: AsyncGenerator<T, TReturn, TNext>
    queueValue: (value: T) => void
    error: (err?: any) => void
    queueError: (err?: any) => void
    return: (value?: TReturn) => void
    queueReturn: (value?: TReturn) => void
}

Creates an async generator and control functions. The async generator yields values, errors, and returns based on the control functions.

  • queueValue queues a value to yielded next
  • error drops all queued values, puts the generator in a "done" state, and has the current pending or next call to next() throw an error
  • queueError puts the generator in a "done" state, and has the current pending or next call to next() throw an error
  • return() drops all queued values, and puts the generator in a "done" state with the passed in value
  • queueReturn() puts the generator in a "done" state with the passed in value
import { deferGenerator } from 'inside-out-async'

const pokedex = deferGenerator()
pokedex.queueValue({ name: 'Pikachu' })
pokedex.queueValue({ name: 'Bulbasaur' })
pokedex.queueValue({ name: 'Charizard' })
pokedex.queueReturn()

for await (const pokemon of pokedex.generator) {
  console.log(pokemon) // { name: 'Pikachu' }, { name: 'Bulbasaur' }, { name: 'Charizard' }
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.2
    209
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.0.2
    209
  • 1.0.1
    41
  • 1.0.0
    0

Package Sidebar

Install

npm i inside-out-async

Weekly Downloads

250

Version

2.0.2

License

MIT

Unpacked Size

16 kB

Total Files

8

Last publish

Collaborators

  • reconbot