Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/jill64/unfurl
- Owner: jill64
- License: mit
- Created: 2023-09-03T03:10:14.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-29T18:09:03.000Z (17 days ago)
- Last Synced: 2024-10-29T20:12:27.834Z (17 days ago)
- Topics: concurrency, promise, utility
- Language: TypeScript
- Homepage: https://npmjs.com/package/@jill64/unfurl
- Size: 350 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @jill64/unfurl
💠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)