This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

promise-cascade
TypeScript icon, indicating that this package has built-in type declarations

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

Dependencies (0)

    Dev Dependencies (6)

    Package Sidebar

    Install

    npm i promise-cascade

    Weekly Downloads

    9

    Version

    0.1.2

    License

    MIT

    Last publish

    Collaborators

    • pedsmoreira