💠 Concurrently wait for a Promise mapped to an object while preserving the type
npm i @jill64/unfurl
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')
// }
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'
// }
// }
// }