Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
/ frogger Public archive

🐸 Chain together a sequence of asynchronous tasks

License

Notifications You must be signed in to change notification settings

the-nuel/frogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@nuel/frogger

Frogger helps to chain together a sequence of tasks, it works like reduce but for asynchronous steps that are more complex than you might want to just throw into an array of functions.

Example

type FruitBasketState = {
    apples: 0,
    oranges: 0,
};

const sequence = new Sequence<FruitBasketState>([
    /* always give people 2 free apples */
    new ExecuteStep({
        name: 'GiveFreeApples',
        execute: (state) => Promise.resolve({
            ...state,
            apples: 2,
        })
    }),

    /* try to get fruit state from a store, but if we fail
     * default to just adding 1 to the current count */
    new ExecuteOrFallbackStep({
        name: 'TryGetOrangeCountOrDefault',
        execute: async (state) => {
            const count = await fruitDB.get('oranges');

            if (count) {
                return { ...state, oranges: count };
            } else {
                return undefined;
            }
        },
        fallback: (state) => Promise.resolve({
            ...state,
            oranges: state.oranges + 1,
        }),
    })
]);

/* run the sequence, given some initial state */
const basket = await sequence.run({ 
    apples: 0, 
    oranges: 0,
});

About

🐸 Chain together a sequence of asynchronous tasks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published