Skip to content
forked from ide/mux

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.

License

Notifications You must be signed in to change notification settings

KillerDesigner/mux

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mux CircleCI

A simple and convenient function that resolves promises in data structures like arrays, objects, Maps, and Sets

What is mux?

Mux is a function that multiplexes promises in nested data structures and resolves them like this:

await mux({
  a: asyncFunction1(),
  b: asyncFunction2(),
});

// Result:
{
  a: result1,
  b: result2,
}

Installing mux

yarn add @exponent/mux

And import it like this:

import mux from '@exponent/mux';

More examples

You can also pass in deeply nested data structures like this:

await mux({
  a: {
    b: asyncFunction1(),
  },
});

// Result:
{
  a: {
    b: result1,
  },
}

Mux supports several standard JavaScript data structures:

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.

await mux([
  Promise.resolve({
    a: asyncFunction1(),
  }),
]);

// Result:
[
  {
    a: result1,
  },
]

Check out the test suite for even more examples.

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 86.7%
  • Shell 13.3%