qpipejs v2.0.0
qpipejs
welcome to heaven
qpipejs is designed to help you to write kiss code.
It is built uppon the concepts of glocal and tasks.
how code looks like
var qpipejs = require('qpipejs')
qpipe = qpipejs({
foo: 'foo',
bar: 'bar'
})
.do(someTask)
.do(otherTask)
.do(thisOneFails, firstFallbackTask, secondFallbackTask, andSoOn)
You do not need to care whether your tasks are synchronous or asynchronous, promises or callbacked functions.
methods
qpipejs([glocal, options])
glocal
is the object to bethis
of every taskoptions
is an object with some options, currently onlyfail
Returns a Qpipe Object
var qpipejs = require('qpipejs')
var qpipe = qpipejs({
foo: 'foo',
bar: 'bar'
}, {
fail: function (error) {
console.log(error)
}
})
qpipejs.use
It is a helper to be used in express
When you do app.use(qpipe.use)
or router.use(qpipe.use)
the res
parameter will be populated with a qpipejs
function that returns a Qpipe
object with some sugar.
qpipe.do(task[, fallback, ...])
Executes task
, passing glocal
as this
If tasks
fails, then executes the next fallback
If there is no more fallbaks
the qpipe fails and options.fail
is executed
var qpipejs = require('qpipejs')
qpipe = qpipejs(glocal)
.do(someTask)
.do(otherTask)
.do(thisOneFails, firstFallbackTask, secondFallbackTask, andSoOn)
The tasks are called with task.call(glocal, resolve, reject)
If the task returns resolve
, it waits until resolve
(or reject
) is called
If the task returns a promise, it waits until the promise is resolved or rejected
If the task returns anything else, the next task is executed.
If the current task throw an exception, calls reject
or reject the promise that it has returned, the next fallback task in the same do
is executed.
qpipe.each(array, callback)
If array
is an Array
, execute callback.call(glocal, element, resolve, reject)
for each element in array
If array
is an Array
, execute callback.call(glocal, element, resolve, reject)
for each element in glocal[array]
qpipe.all(task, task, ...)
It executes all tasks like that task.call(glocal, resolve, reject)
and waits until every one is resolved, or anyone is rejected.
qpipe.race(task, task, ...)
It executes all tasks like that task.call(glocal, resolve, reject)
and waits until anyone is resolved or rejected.
qpipe.wait()
It returns the promise at that point.
It is the same that qpipe.promise
qpipe.response('foo', 'bar', ...)
Express sugar It response the request with a object that has the keys passed as arguments, and theirs values on the glocal.
If 'status'
is passed, it set the glocal.status
status
qpipe.ok()
Express sugar
It response the request with {"ok": true}
and status 200
attributes
qpipe.glocal
It is the object that is passed to every task executed by the qpipe
qpipe.options
It is the object with the options passed.
currently only fail
is used
qpipe.options.fail(error)
It is called when any task throw an exception or rejects
If you do not set your own fail
function, the default ones are called.
In express: it response the request with status 500 (or error.status) and the json of the error
promise
It is the promise of the last Qpipe method called. Not of the task called.