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

promiseflow

v1.0.11

Published

provides series and parallel flow control for promise. using Array to represent tasks in series. using Object to represent tasks in parallel.

Downloads

36

Readme

promiseflow

provides series and parallel flow control for promise. using Array to represent tasks in series. using Object to represent tasks in parallel.

series_parallel_input

install

npm install promiseflow

Example

series demo

series

import flowFactory from 'promiseflow'
import Promise from 'es6-promise'
const runFlow = flowFactory(Promise)

const initData = 1
const fun1 = function (data) {
  // expect(data).to.be.equal(1)
  return data + 1
}
const fun2 = function (data) {
  // expect(data).to.be.equal(2)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 2)
    }, 50)
  })
}
const arr = [fun1, fun2]
runFlow(arr, initData).then(data => {
  // expect(data).to.be.equal(4)
  done()
})

parallel demo

parallel

import flowFactory from 'promiseflow'
import Promise from 'es6-promise'
const runFlow = flowFactory(Promise)

const initData = 1
const fun1 = function (data) {
  // expect(data).to.be.equal(1)
  return data + 1
}
const fun2 = function (data) {
  // expect(data).to.be.equal(1)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 2)
    }, 50)
  })
}
const fun3 = function (data) {
  // expect(data).to.be.equal(1)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 3)
    }, 50)
  })
}
const obj = { fun1, fun2, fun3 }
let promise = runFlow(obj, initData)
promise.then(data => {
  // expect(data.fun1).to.be.equal(2)
  // expect(data.fun2).to.be.equal(3)
  // expect(data.fun3).to.be.equal(4)
  // done()
})

series + parallel demo

series_parallel

import flowFactory from 'promiseflow'
import Promise from 'es6-promise'
const runFlow = flowFactory(Promise)

const initData = 1
const fun1 = function (data) {
  // expect(data).to.be.equal(1)
  return data + 1
}
const fun2 = function (data) {
  // expect(data).to.be.equal(2)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 2)
    }, 50)
  })
}
const fun3 = function (data) {
  // expect(data).to.be.equal(2)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 3)
    }, 50)
  })
}
const fun4 = function (data) {
  // expect(data.fun2).to.be.equal(4)
  // expect(data.fun3).to.be.equal(5)
  return data.fun2 + data.fun3
}
const arr = [fun1, {
  fun2, fun3
}, fun4]
runFlow(arr, initData).then(data => {
  // expect(data).to.be.equal(9)
  // done()
})

sub flows demo

sub_flow

import flowFactory from 'promiseflow'
import Promise from 'es6-promise'
const runFlow = flowFactory(Promise)

const inData = 1
const subFlows = [{
  sub1: ret => {
    return ret + 1
  },
  sub2: ret => {
    return ret + 2
  }
}, ret => {
  return ret.sub1 + ret.sub2
}]
const fun1 = function (data) {
  // expect(data).to.be.equal(1)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        __flow__: true,
        flows: subFlows,
        inData: data + 1
      })
    }, 50)
  })
}
const fun2 = function (data) {
  // expect(data).to.be.equal(7)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data + 2)
    }, 50)
  })
}
const arr = [fun1, fun2]
runFlow(arr, inData).then(data => {
  // expect(data).to.be.equal(9)
  // done()
})

use callback to handle each task

import flowFactory from 'promiseflow'
import Promise from 'es6-promise'
const runFlow = flowFactory(Promise)

const arr = ['series.png', {
  img1: 'parallel.png',
  img2: 'series_parallel.png'
}, 'sub_flow.png']
const callback = (val) => {
  if (typeof val === 'string') {
    return function () {
      return new Promise((resolve, reject) => {
        const img = document.createElement('img')
        img.id = val
        img.onload = () => {
          img.onload = null
          img.onerror = null
          resolve(img.name)
        }
        img.onerror = () => {
          img.onload = null
          img.onerror = null
          reject(new Error(`${val} load failed`))
        }

        img.src = `/base/img/${val}`

        document.body.appendChild(img)
      })
    }
  }

  return val
}
runFlow(arr, '', callback).then(data => {
  // expect(true).to.be.equal(true)
  done()
})

License

MIT