NoPromise

NoPromise

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

Normal usage

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
})

Catch errors

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
})

Deferred usage

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 function (err: <Error>, res: any, cb: Function). Never forget to call cb(err: <Error>, res: any) inside fn

Source:

end(callback)

End the chain

Parameters:
Name Type Description
callback function

function (err: <Error>, res: any)

Source:

then(task)

Chain the next async function

Parameters:
Name Type Description
task function

async function function (res: any, cb: Function). Never forget to call cb(err: <Error>, res: any) inside fn

Source: