@jill64/unfurl
TypeScript icon, indicating that this package has built-in type declarations

2.0.6 • Public • Published

@jill64/unfurl

npm-version npm-license npm-download-month npm-min-size ci.yml

💠 Concurrently wait for a Promise mapped to an object while preserving the type

Install

npm i @jill64/unfurl

Usage

When passed an object containing promises, it will wait until all promises are resolved, keeping the keys and values.

import { unfurl } from '@jill64/unfurl'

const result = await unfurl({
  number: Promise.resolve(1),
  string: Promise.resolve('Test'),
  boolean: true,
  object: Promise.resolve({
    key: 'value'
  })
})
// Return Value
// {
//   number: 1,
//   string: 'Test',
//   boolean: true,
//   object: {
//     key: 'value'
//   }
// }

If you pass any number of promises after the second argument, it will wait until they are resolved.
However, the return value is not available.

import { unfurl } from '@jill64/unfurl'

const result = await unfurl(
  {
    set: Promise.resolve(new Set(['A', 'B', 'C'])),
    date: Promise.resolve(new Date('2000-01-01'))
  },
  new Promise((resolve) => setTimeout(resolve, 100)),
  new Promise((resolve) => setTimeout(resolve, 200)),
  new Promise((resolve) => setTimeout(resolve, 300))
)
// Return Value
// {
//   set: new Set(['A', 'B', 'C']),
//   date: new Date('2000-01-01')
// }

unfurlSettled

The settled version uses Promise.allSettled internally and waits until all Promises have completed. Also, the return value type will be the value wrapped in PromiseSettledResult.

import { unfurlSettled } from '@jill64/unfurl'

const result = await unfurlSettled({
  number: Promise.resolve(1),
  string: Promise.resolve('Test'),
  boolean: true,
  object: Promise.resolve({
    key: 'value'
  })
})
// Return Value
// {
//   number: {
//     status: 'fulfilled',
//     value: 1
//   },
//   string: {
//     status: 'fulfilled',
//     value: 'Test'
//   },
//   boolean: {
//     status: 'fulfilled',
//     value: true
//   },
//   object: {
//     status: 'fulfilled',
//     value: {
//       key: 'value'
//     }
//   }
// }

License

MIT

Package Sidebar

Install

npm i @jill64/unfurl

Weekly Downloads

865

Version

2.0.6

License

MIT

Unpacked Size

6.79 kB

Total Files

9

Last publish

Collaborators

  • jill64