包详细信息

simple-asyncify

lydell65MIT不推荐使用1.0.0

Useless package

Turns sync functions into Node.js-style async ones.

async, asyncify, callback

自述文件

Overview Build Status

Turns sync functions into Node.js-style async ones.

Inspired by node-asyncify.

var asyncify = require("simple-asyncify")

function addSync(a, b) {
  if (typeof a !== "number" || typeof b !== "number") {
    throw new TypeError("add requires two numbers")
  }
  return a + b
}

var add = asyncify(addSync)

add(1, 2, function(error, sum) {
  console.log("The sum is: " + sum)
})

add(1, function(error, sum) {
  console.log(error.message)
})

console.log("Let’s do some math!")

// Let’s do some math!
// The sum is: 3
// add requires two numbers

Installation

npm install simple-asyncify

var asyncify = require("simple-asyncify")

Usage

asyncify(syncFn)

Returns an async function that accepts the same arguments as syncFn plus a callback. It runs syncFn with the arguments. If syncFn throws an error, the callback is invoked with that error. Otherwise the callback is invoked with null and the return value of syncFn.

setImmediate is used to make the function async.

License

The X11 (“MIT”) License.

更新日志

Version 1.0.0 (2015-08-22)

  • Fixed: No longer catches errors in the callback. Thanks to @charlierudolph!

Version 0.1.0 (2014-03-20)

Initial version.