All Projects → JiLiZART → sequence-as-promise

JiLiZART / sequence-as-promise

Licence: MIT license
Executes array of functions as sequence and returns promise

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to sequence-as-promise

indexeddb-orm
Indexed DB ORM
Stars: ✭ 53 (+130.43%)
Mutual labels:  promise
PiedPiper
A small set of classes and functions to make easy use of Futures, Promises and async computation in general. All written in Swift for iOS 10+, WatchOS 3, tvOS and Mac OS X apps.
Stars: ✭ 44 (+91.3%)
Mutual labels:  promise
cakephp-sequence
CakePHP plugin for maintaining a contiguous sequence of records
Stars: ✭ 41 (+78.26%)
Mutual labels:  sequence
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (-21.74%)
Mutual labels:  promise
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+226.09%)
Mutual labels:  promise
rmfr
Node.js implementation of rm -fr – recursive removal of files and directories
Stars: ✭ 23 (+0%)
Mutual labels:  promise
examples
MetaCall Examples - A collection of use cases and examples to be deployed in MetaCall.
Stars: ✭ 18 (-21.74%)
Mutual labels:  functions
best-queue
Queue in runtime based promise
Stars: ✭ 26 (+13.04%)
Mutual labels:  promise
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (+47.83%)
Mutual labels:  promise
shogun
Shogun: Functions As Commands for the true samurai developer.
Stars: ✭ 23 (+0%)
Mutual labels:  functions
Start-Stop
A python game for beginners. This game is very important for developers.
Stars: ✭ 14 (-39.13%)
Mutual labels:  functions
co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (-26.09%)
Mutual labels:  promise
repromised
🤝 Declarative promise resolver as a render props component
Stars: ✭ 20 (-13.04%)
Mutual labels:  promise
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+304.35%)
Mutual labels:  promise
relaks
Asynchrounous React component
Stars: ✭ 49 (+113.04%)
Mutual labels:  promise
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (+43.48%)
Mutual labels:  promise
promise-abortable
Promise lib for aborting in chain.
Stars: ✭ 19 (-17.39%)
Mutual labels:  promise
p-cache
Decorator to memoize the results of async functions via lru-cache.
Stars: ✭ 21 (-8.7%)
Mutual labels:  promise
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (-8.7%)
Mutual labels:  promise
serverless-scaleway-functions
Plugin for Serverless Framework to allow users to deploy their serverless applications on Scaleway Functions
Stars: ✭ 58 (+152.17%)
Mutual labels:  functions

Build Status Code Climate

Sequence as Promise

It's zero dependency and lightweight function that allows execute array of functions and promises in sequence and returns Promise

What it do?

Behavior very similar to Promise.all. It's executes promises with functions one by one and returns promise with array of results

this code

promise1.then(() => promise2.then(() => promise3.then(callback))).then(done);

equivalent to

const sequence = require('sequence-as-promise');

sequence([promise1, promise2, promise3, callback]).then(done);

How to install

with npm

npm i --save sequence-as-promise

with yarn

yarn add sequence-as-promise

Basic usage

We have array of functions with promises, and we need to execute all that functions in sequence

All functions in sequence accepts two arguments, (prevResult, results) => {}

  • prevResult the result of previous function or promise call
  • results an array of results from previous functions or promises calls
const sequence = require('sequence-as-promise');
sequence([
    Promise.resolve({status: true}),
    (prevResult/*{status: true}*/, results) => {
        return {moveCircleToMiddle: true};
    },
    (prevResult/*{moveCircleToMiddle: true}*/, results) => {
        return {showGrayCircle: true};
    },
    (prevResult/*{showGrayCircle: true}*/, results) => {
        return {showMicrophone: true};
    }
]).then((results) => console.log('all done', results))

Functions that returns promise

Most standard use case is a fetch dependant data one by one

const sequence = require('sequence-as-promise');
sequence([
    fetchUser(32),
    (user) => {
        if (user && user.id === 1) {
            return fetchAdminUrls(user.id);
        }

        return fetchUserUrls(user.id);
    },
    (urls) => {//previous fetch resolved and passed as argument
        return urls.map(makeLink)
    }
]).then((results) => {
    const [user, _, links] = results;
    
    renderHTML(user, links);
});

Handle errors

Any function or promise in sequence can throw an error, so we need to handle it

const sequence = require('sequence-as-promise');
sequence([
    fetchUser(32),
    (user) => {
        if (user && user.id === 1) {
            return fetchAdminUrls(user.id); //for instance this fetch throws server error
        }

        return fetchUserUrls(user.id);
    },
    (urls) => { //this will not be executed, because previous promise thorws error
        return urls.map(makeLink); 
    }
]).then(
    (results) => {
        const [user, _, links] = results;
        
        renderHTML(user, links);
    },
    (results) => {
        const error = results.pop(); //last item in results always be an error

        renderError(error);
    }
);

More examples

But, if we need to call all that functions with primitive values between them (why not?).

const sequence = require('sequence-as-promise');
sequence([
    () => {
        return {moveCircleToMiddle: true};
    },
    100,
    (prevResult/*100*/, results) => {
        return {showGrayCircle: prevResult};
    },
    (prevResult/*{showGrayCircle: 100}*/, results) => {
        return {showMicrophone: true};
    },
    500,
    (prev, values) => { // prev == 500
        return {moveCircleToTop: true};
    }
]).then((results) => console.log('all done', results))

Or we have Promises in that array of functions.

const sequence = require('sequence-as-promise');
sequence([
    () => new Promise((resolve, reject) => {
        resolve({moveCircleToMiddle: true});
    }),
    () => {
        return {showGrayCircle: true};
    }
]).then(() => console.log('all done'))
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].