Giter Club home page Giter Club logo

yubikiri's Introduction

ゆびきり (Yubikiri)

ゆびきり (yubikiri, or "pinky swear") is a library to facilitate Promise-based data loading in JavaScript. You provide an object filled with plain values, Promises, or functions, and Yubikiri will order the dependencies and return a Promise that resolves to an object with all the sub-Promises resolved.

Installation

Yubikiri makes use of Proxies, Reflect, and async/await. Node.js v7.6.0 is the first version that supports Yubikiri.

With npm:

npm install [--save] yubikiri

Usage

Yubikiri exposes a single function that takes a JavaScript object. The keys of this object are names to be used in the result, and the values are Promises that will be resolved.

const data = await yubikiri({
  one: Promise.resolve(1),
  two: Promise.resolve(2)
})

// data === { one: 1, two: 2 }

You can also specify functions that depend on other values being calculated at the same time. Yubikiri will take care of ensuring the values that depend on each other are resolved correctly. Each function is only calculated once, even if more than one other function depends on its value.

const data = await yubikiri(query => ({
  one: Promise.resolve(1),
  two: 2,
  three: (query) => {
    return query.one.then(one => {
      return query.two.then(two => {
        return one + two
      })
    })
  }
}))

// data === { one: 1, two: 2, three: 3 }

If you're using async/await, this pattern can be a little nicer:

const data = await yubikiri(query => ({
  one: Promise.resolve(1),
  two: Promise.resolve(2),
  three: async (query) => {
    const [one, two] = await Promise.all([query.one, query.two])
    return one + two
  }
}))

// data === { one: 1, two: 2, three: 3 }

If any of the specified Promises reject, the overall Promise returned from Yubikiri will also reject with the same value.

Yubikiri will try to detect infinite loops and return a rejected Promise with an error message that describes the dependency loop.

yubikiri's People

Contributors

binarymuse avatar kuychaco avatar

Stargazers

 avatar

Watchers

 avatar  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.