npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequence-as-promise

v1.2.0

Published

Executes array of functions and promises as sequence and returns promise

Downloads

564

Readme

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, but all promises or functions executes in sequence. Function executes promises with functions one by one and returns promise withresults array

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 these functions 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 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 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'))