Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/binarymuse/yubikiri
Fetch data using Promises. Pinky swear!
https://github.com/binarymuse/yubikiri
data-fetching javascript promises
Last synced: 29 days ago
JSON representation
Fetch data using Promises. Pinky swear!
- Host: GitHub
- URL: https://github.com/binarymuse/yubikiri
- Owner: BinaryMuse
- License: mit
- Created: 2017-02-24T18:55:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:16:42.000Z (almost 2 years ago)
- Last Synced: 2024-04-09T01:05:28.791Z (7 months ago)
- Topics: data-fetching, javascript, promises
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/yubikiri
- Size: 137 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ゆびきり (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.
```javascript
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.
```javascript
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:
```javascript
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.