神刀安全网

opinionated async serial execution

k-pipeline

Executes a bunch of callback styled functions serially

This is an adhoc/opinionated version of the popular async.<waterfall|serial>

example – flow control

npm i k-pipeline

const Pipeline = require('k-pipeline')  let pipeline = Pipeline.create([      // state is just a new javascript object     (state, next, stop) => { next() },      // stop the execution in the middle -> jump directly to final callback     (state, next, stop) => { stop() },       // this will not be executed     (state, next, stop) => { next() }  ])  pipeline.run((err, state) => { })

example – custom state

const Pipeline = require('k-pipeline')  let aState = { foo: 'bar' } let pipeline = Pipeline.create([     (state, next, stop) => {          console.log(state === aState) // true         next()     } ])  // override constructor state pipeline.run(aState, (err, state) => {     console.log(state === aState) // true })

example – reuse the pipeline

const Pipeline = require('k-pipeline')  let aState = { foo: 'bar' } let pipeline = Pipeline.create([     (state, next, stop) => { next() } ])  // state will reset to a new javascript object each run() pipeline.run((err, state) => {     pipeline.run((err, state) => {      }) })

example – error in the pipe

const Pipeline = require('k-pipeline')  let error = new Error() let pipeline = Pipeline.create([     (state, next, stop) => { next(error) },     (state, next, stop) => { next() } // will not be executed ])  pipeline.run((err, state) => {     console.log(error === err) //true })

stuff you can’t do

call a callback twice

const Pipeline = require('k-pipeline')  let pipeline = Pipeline.create([     (state, next, stop) => {          next()         next() // throws an error     } ])  pipeline.run((err, state) => {})

call run() twice in the same tick

const Pipeline = require('k-pipeline')  let pipeline = Pipeline.create([     (state, next, stop) => { next() } ])  pipeline.run((err, state) => {}) pipeline.run((err, state) => {}) // throws an error

install

With npm do:

npm install k-pipeline 

license

MIT © yaniv kessler

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » opinionated async serial execution

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址