Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jill64/unfurl

💠 Concurrently wait for a Promise mapped to an object while preserving the type
https://github.com/jill64/unfurl

concurrency promise utility

Last synced: 5 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# @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

```sh
npm i @jill64/unfurl
```

## Usage

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

```js
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.

```js
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`.

```js
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](LICENSE)