GithubHelp home page GithubHelp logo

anthrax3 / mux Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ide/mux

0.0 0.0 0.0 54 KB

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: MIT License

Shell 17.57% JavaScript 82.43%

mux's Introduction

mux CircleCI codecov

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 @expo/mux

And import it like this:

import mux from '@expo/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.

Interesting use cases

If you've discovered a particularly interesting way to use mux, add it here and send a PR to share it.

mux's People

Contributors

ide avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.