Creates a promise that waits for the promises in nested data structures and resolves to data structures of the same form. It recursively traverses the input data structure and multiplexes its promises.
npm install @expo/muxMux is a function that multiplexes promises in nested data structures and resolves them like this:
``js
await mux({
a: asyncFunction1(),
b: asyncFunction2(),
});
// Result:
{
a: result1,
b: result2,
}
`
`sh`
yarn add @expo/mux
And import it like this:
`js`
import mux from '@expo/mux';
You can also pass in deeply nested data structures like this:
`js
await mux({
a: {
b: asyncFunction1(),
},
});
// Result:
{
a: {
b: result1,
},
}
`
Mux supports several standard JavaScript data structures:
`js
await mux(new Set([
asyncFunction1(),
asyncFunction2(),
]));
// Result:
new Set([
result1,
result2,
])
`
And if your promises themselves result in data structures, mux will recurse into them and resolve the nested promises.
`js
await mux([
Promise.resolve({
a: asyncFunction1(),
}),
]);
// Result:
[
{
a: result1,
},
]
``
Check out the test suite for even more examples.
If you've discovered a particularly interesting way to use mux, add it here and send a PR to share it.