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

queue-orchestrator

v1.0.6

Published

A basic module for sequencing and executing tasks based in promises.

Downloads

8

Readme

Build Status NPM version

Queue-orchestrator

A basic and simple module for sequencing and executing tasks based in promises, this module can reverse tasks if something has failed.

You can chain and execute special promises if something is wrong, also if you set "break:true" the chain stops and start to execute the reverse functions backward.

Npm Install

npm install queue-orchestrator --save

Javascript Usage

    const Orchestrator = require("queue-orchestrator");
   
    const orchestrator = new Orchestrator();
    
    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          resolve("OK");
        }); 
      }
    });
    orchestrator.start().then(res=>console.log(res[0].result));
    //OK

Using reverse concept if a promise is rejected

Example 1

const Orchestrator = require("queue-orchestrator");
const axios = require('axios');
 
const orchestrator = new Orchestrator();

orchestrator.add({
  reverse(){
    return new Promise((resolve, reject) => {
      resolve("The process has reversed");
    }); 
  },
  run(){
    return axios.get('https://jsonplaceholder.typicode.com/posts/1');
  }
});

orchestrator.add({
  reverse(){
    return new Promise((resolve, reject) => {
      resolve("The 2d process has reversed");
    }); 
  },
  run(){
    return axios.get('https://jsonplaceholder.typicode.com/posts/1');
  }
});

orchestrator.add({
  options: {
    break: true
  },
  run(){
    return Promise.reject('Rejected on purpose');
  }
})     

orchestrator.start().then(res=>{
  console.log(res[0].reverse);
  //The process has reversed
  
  console.log(`${res[0].reversed} - ${res[1].reversed} - ${res[2].reversed}`);
  //true - true - false

  console.log(res[1].result.data);
  /*
   * Response : { userId: 1,
   *   id: 1,
   *   title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
   *   body: 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut qu
   *   as totam\nnostrum rerum est autem sunt rem eveniet architecto' }
   */

  console.log(res[2]);
  /*
   * Response: { success: false,
   * result: 'Rejected on purpose',
   * pos: 2,
   * reversed: false,
   * reverse: 'There is not reverse function' }
   */
});

Example 2

    const Orchestrator = require("queue-orchestrator");
    
    const orchestrator = new Orchestrator();
    
    orchestrator.add({
      reverse(){
        return new Promise((resolve, reject) => {
          resolve("The process has reversed");
        }); 
      },
      run(){
       return {data: "something"}; 
      }
    });
 
    orchestrator.add({
      reverse(){
        return new Promise((resolve, reject) => {
          resolve("The 2d process has reversed");
        }); 
      },
      run(){
       return {data: "something"}; 
      }
    });

    orchestrator.add({
      options: {
        break: true
      },
      run(){
        return new Promise((resolve, reject) => {
          reject("Rejected and reversing");
        }); 
      }
    });

    orchestrator.start().then(res=>{
      console.log(res[0].reversed===true);
      //true
      console.log(res[1].reversed===true);
      //true
      console.log(res[1].reverse);
      //The 2d process has reversed
    });

Options

General Orchestrator features.

| F(X) | Params | Return | Detail | |---------------|-----------------|---------|---------| | start | | promise | This method starts to chain javascript promises| | add | option object | void | This method add a promise|

Add() method options.

| Properties | Type | Detail | |-------------------------|---------|---------| | options: {break: true}| boolean | When the promise failed it will start to execute reversing promises backward if break is true| | run | function | Main method, you have to return a promise here| | reverse | function | Revere method, you have to return a promise here|

General Usage

describe('add task', () => {

  it('add() should be return undefined', () => {
    const orchestrator = new Orchestrator();
    expect(orchestrator.add({})).to.be.a('undefined');
  });

  it('tasks should be return 1 element if an item is passed', () => {
    const orchestrator = new Orchestrator();
    orchestrator.add({});
    expect(orchestrator.tasks.length).to.equal(1);
  });

});

describe('start tasks', () => {

  it('start should be return a promise', () => {
    const orchestrator = new Orchestrator();
    return expect(orchestrator.start()).to.be.a('promise'); 
  });

  it('starts promise should be resolve with a list of tasks responses', async () => {
    const orchestrator = new Orchestrator();
    orchestrator.add({});
    orchestrator.add({});

    const res = await orchestrator.start()
    return expect(res.length).to.equal(2);
  });
  
  it('starts promise should be resolve with an array of objects', async () => {
    const orchestrator = new Orchestrator();
    orchestrator.add({});
    orchestrator.add({});

    const res = await orchestrator.start()
    return expect(res).to.be.an('array');
  });


});

describe('tasks runner', () => {

  it('tasks must be executed', async() => {
    const orchestrator = new Orchestrator();
    orchestrator.add({
      run(){
       return {success: true}; 
      }
    });
    const res = await orchestrator.start();

    return expect(res[0]).to.have.own.property('success', true);
  });

  it('start process must be catch the promises resolve', async() => {
    const orchestrator = new Orchestrator();
    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          resolve("OK");
        }); 
      }
    });
    const res = await orchestrator.start();
    return expect(res[0]).to.have.own.property('result', "OK");
  });

  it('start process must be catch the promises reject', async() => {
    const orchestrator = new Orchestrator();
    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          reject(false);
        }); 
      }
    });
    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          reject(new Error("500xerr"));
        }); 
      }
    });

    const res = await orchestrator.start();
    
    return expect(res[0]).to.have.own.property('success', false) 
      && expect(res[1].result).to.have.own.property('message', "500xerr");
  });

});


describe('task break', () => {

  it('when the task is broken and its property break is true the process is finish', async() => {
    const orchestrator = new Orchestrator();
    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          resolve("OK");
        }); 
      }
    });

    orchestrator.add({
      options: {
        break: true
      },
      run(){
        return new Promise((resolve, reject) => {
          reject("some error");
        }); 
      }
    });

    orchestrator.add({
      run(){
        return new Promise((resolve, reject) => {
          resolve("OK");
        }); 
      }
    });
    
    const res = await orchestrator.start();
    return expect(res[2]).to.be.undefined;
  });


  it('should be execute only the first task', async() => {
    const orchestrator = new Orchestrator();
    orchestrator.add({
      options: {
        break: true
      },
      run(){
        return new Promise((resolve, reject) => {
          reject("Rejected");
        }); 
      }
    });

    orchestrator.add({});
    orchestrator.add({});

    const res = await orchestrator.start();
    return expect(res[2]).to.be.undefined 
      && expect(res[0]).to.have.own.property('result', "Rejected"); 
  });

});


describe('task reverse', () => {

  it('when something is wrong the reverse function has to execute for all the functions that have passed', async() => {
    const orchestrator = new Orchestrator();
    
    orchestrator.add({
      reverse(){
        return new Promise((resolve, reject) => {
          resolve("The process has reversed");
        }); 
      },
      run(){
       return {data: "something"}; 
      }
    });
 
    orchestrator.add({
      reverse(){
        return new Promise((resolve, reject) => {
          resolve("The 2d process has reversed");
        }); 
      },
      run(){
       return {data: "something"}; 
      }
    });

    orchestrator.add({
      options: {
        break: true
      },
      run(){
        return new Promise((resolve, reject) => {
          reject("Rejected and reversing");
        }); 
      }
    });

    const res = await orchestrator.start();

    return expect(res[0]).to.have.own.property('reversed', true)
      && expect(res[1]).to.have.own.property('reversed', true)
      && expect(res[1]).to.have.own.property('reverse', "The 2d process has reversed")

  });

});

Tests

npm test

License

MIT