new NoPromise(arg)
This is not a Promise
.
Chain callback functions with .then(function (res, cb))
and execute them
as soon as previous callbacks have finished.
Catch passed or thrown errors with .catch(function (err, res, cb))
as they may occur.
End the chain with .end(function (err, res))
.
If errors are thrown inside a task
they are catched and can be processed attaching
.catch()
or .end()
to the chain.
This method is similar to connect
but allows adding tasks
on the go through chaining.
Parameters:
Name | Type | Description |
---|---|---|
arg |
Any | initial argument which is passed to first chain |
- Source:
Examples
var arr = []
var n = new NoPromise(arr)
n.then((res, cb) => {
res.push(1)
cb(null, res)
}).then((res, cb) => {
res.push(2)
cb(null, res)
}).end((err, res) => {
//> err = null
//> res = [1, 2]
//> (arr ==== res) = true
})
var arr = []
var n = new NoPromise(arr)
n.then((res, cb) => {
res.push(1)
cb(null, res)
}).then((res, cb) => {
res.push(2)
cb('err1', res) // <-- cause an error
}).catch((err, res, cb) => { // catches err1
res.push(err)
cb(null, res) // <-- continue normally
}).then((res, cb) => {
res.push(3)
cb(null, res)
}).catch((err, res, cb) => { // jumps over, as there is no error in the chain
res.push(4)
cb(null, res)
}).then((res, cb) => {
res.push(5)
cb('err2', res) // <-- next error
}).end((err, res) => {
//> err = 'err2'
//> res = [1, 2, 'err1', 3, 5]
//> (arr ==== res) = true
})
var arr = []
// creates a new instance passing `arr`
var n = new NoPromise(arr)
// execute the first async method
n.then((res, cb) => {
res.push(1)
cb(null, res)
})
// take a time off
setTimeout(() => {
// continue processing
n.then((res, cb) => {
res.push(2)
cb(null, res)
}).end((err, res) => {
//> err = null
//> res = [1, 2]
//> (arr ==== res) = true
})
}, 10)
Methods
catch(trap)
Catch any previous errors from the chain
Parameters:
Name | Type | Description |
---|---|---|
trap |
function | async function |
- Source:
end(callback)
End the chain
Parameters:
Name | Type | Description |
---|---|---|
callback |
function |
|
- Source:
then(task)
Chain the next async function
Parameters:
Name | Type | Description |
---|---|---|
task |
function | async function |
- Source: