Nurturing Palpable Magnificence

npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

promise-cascade

0.1.2 • Public • Published

PromiseCascade

npm version Build Status Code Climate Test Coverage

Easy to use library for cascading promises. If you're looking for a way to use the response from one promise into another, this library is meant for you.

Designed while working on the Premiere Library, PromiseCascade thrives helping keep concerns separate in a clean manner.

Installation

npm install promise-cascade --save

API

  • stack: Stores pushed functions and arguments
  • push(fn: PromiseFunction, ...args: any[]): this: Pushes a function to the stack (this function must return a Promise)
  • play(): Promise<any>: Calls the pushed function with given args + callback
  • clear(): Removes all values from the stack

Example

This examples are written in TypeScript. To use with ES6, just remove typings.

Pushing and Playing

import PromiseCascade, {PromiseFunction} from 'promise-cascade';
 
const addAwesomeness = async (callback: PromiseFunction) => {
    const callbackResponse = await callback();
    return 'Awesome ' + callbackResponse;
};
 
const getName = async (name: string) => name; 
 
const cascade = new PromiseCascade();
const promise = cascade
    .push(addAwesomeness)
    .push(getName, 'John Doe')
    .play();
 
promise.then((result) => {
   console.log(result); // 'Awesome John Doe' 
});

Real World Example - Extending PromiseCascade

This is a simplified version of Premiere's StoreCacheCascade class.

import PromiseCascade, {PromiseFuntion} from 'promise-cascade';
import {Cache, Model} from 'premiere';
import axios from 'axios';
 
export default class CacheCascade<T> extends PromiseCascade {
    promisesCache: Cache<Promise<any>> = new Cache<Promise<any>>();
    objectsCache: Cache<T> = new Cache<T>();
    
    promiseCallback(name: string, callback: PromiseFunction): Promise<T> {
        const cached = this.promisesCache.get(name);
        if (cached) {
            return cached;
        }
 
        return this.promisesCache.set(name, callback());
    }
 
    promise(name: string): this {
        return this.push(this.promiseCallback.bind(this), name);
    }
 
    async objectCallback(key: any, callback: PromiseFunction): Promise<T> {
        const cached = this.objectsCache.get(key);
        if (cached) {
            return cached;
        }
 
        const result: T = await callback();
        return this.objectsCache.set(result.key, result);
    }
 
    object(key: any): this {
        return this.push(this.objectCallback.bind(this), key);
    }
}
 
const fetchModel = (url: string): async Promise<Model> => {
    const response = await axios.get(url);
    return Model.make(response.data);
};
 
const get(key: any): Promise<Model> => {
    const cascade = new CacheCascade<Model>();
    
    return cascade
        .promise('get/' + key) // Avoid making the same request before the first one has arrived
        .object('key') // Avoid redoing a request if this model is already cached
        .push(fetchModel, 'http://api.com/model/' + key) // Http request to the API
        .play();
};
 
get(1);

Resources

For more about how Promise works, check out Dave Atchley's article

License

MIT

install

npm i promise-cascade

Downloadsweekly downloads

38

version

0.1.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability