JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 67
  • Score
    100M100P100Q76867F
  • License MIT

Run async & sync callbacks

Package Exports

  • run-callback

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (run-callback) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

run-callback

Run async or sync callbacks, such as gulp tasks.

version status coverage dependencies devDependencies

The main ideas are borrowed from orchestrator.

Usage

var run = require('run-callback')
var thunkify = run.thunkify

promise = run(callback, ...args)

Run callback with args, and return a promise to fetch the results, which is always an Array.

callback

Type: Function

callback can be made asynchronous if it does one of the following:

Return a promise
run(function () {
  return new Promise(function (resolve) {
    process.nextTick(function () {
      resolve('done')
    })
  })
})
.then(function (res) {
  // 'done'
  console.log(res[0])
})
Return a stream
var Readable = require('stream').Readable

var src = ['beep', '\n', 'boop']
run(function () {
  var stream = createStream(src)
  stream.pipe(process.stdout)
  return stream
})
.then(function () {
  console.log('\n')
  // `[]`
  console.log(src)
})

function createStream(source) {
  var rs = Readable()
  rs._read = function () {
    if (source.length) {
      this.push(source.pop())
    } else {
      this.push(null)
    }
  }
  return rs
}
Accept one more argument than declared
run(function (a, b, next) {
  process.nextTick(function () {
    next(null, a + b, a - b)
  })
}, 2, 1)
.then(function (res) {
  // `[3, 1]`
  console.log(res)
})

run.thunkify(fn, context)

Return a new function to run fn later with a list of arguments.

var task = run.thunkify(function (a, b, next) {
  process.nextTick(function () {
    next(null, a + b, a - b)
  })
})

task(2, 1).then(function (res) {
  // `[3, 1]`
  console.log(res)
})