pax_global_header00006660000000000000000000000064131075406630014517gustar00rootroot0000000000000052 comment=aa21cfb64f47238c764d258d0faf7366a8487e36 thenify-3.3.0/000077500000000000000000000000001310754066300131705ustar00rootroot00000000000000thenify-3.3.0/.gitignore000066400000000000000000000000561310754066300151610ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage thenify-3.3.0/.travis.yml000066400000000000000000000002341310754066300153000ustar00rootroot00000000000000node_js: - "4" - "6" language: node_js script: "npm run-script test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" thenify-3.3.0/History.md000066400000000000000000000001531310754066300151520ustar00rootroot00000000000000 3.3.0 / 2017-05-19 ================== * feat: support options.multiArgs and options.withCallback (#27) thenify-3.3.0/LICENSE000066400000000000000000000021411310754066300141730ustar00rootroot00000000000000 The MIT License (MIT) Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. thenify-3.3.0/README.md000066400000000000000000000073101310754066300144500ustar00rootroot00000000000000 # thenify [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependency Status][david-image]][david-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] Promisify a callback-based function using [`any-promise`](https://github.com/kevinbeaty/any-promise). - Preserves function names - Uses a native promise implementation if available and tries to fall back to a promise implementation such as `bluebird` - Converts multiple arguments from the callback into an `Array`, also support change the behavior by `options.multiArgs` - Resulting function never deoptimizes - Supports both callback and promise style An added benefit is that `throw`n errors in that async function will be caught by the promise! ## API ### fn = thenify(fn, options) Promisifies a function. ### Options `options` are optional. - `options.withCallback` - support both callback and promise style, default to `false`. - `options.multiArgs` - change the behavior when callback have multiple arguments. default to `true`. - `true` - converts multiple arguments to an array - `false`- always use the first argument - `Array` - converts multiple arguments to an object with keys provided in `options.multiArgs` - Turn async functions into promises ```js var thenify = require('thenify'); var somethingAsync = thenify(function somethingAsync(a, b, c, callback) { callback(null, a, b, c); }); ``` - Backward compatible with callback ```js var thenify = require('thenify'); var somethingAsync = thenify(function somethingAsync(a, b, c, callback) { callback(null, a, b, c); }, { withCallback: true }); // somethingAsync(a, b, c).then(onFulfilled).catch(onRejected); // somethingAsync(a, b, c, function () {}); ``` or use `thenify.withCallback()` ```js var thenify = require('thenify').withCallback; var somethingAsync = thenify(function somethingAsync(a, b, c, callback) { callback(null, a, b, c); }); // somethingAsync(a, b, c).then(onFulfilled).catch(onRejected); // somethingAsync(a, b, c, function () {}); ``` - Always return the first argument in callback ```js var thenify = require('thenify'); var promise = thenify(function (callback) { callback(null, 1, 2, 3); }, { multiArgs: false }); // promise().then(function onFulfilled(value) { // assert.equal(value, 1); // }); ``` - Converts callback arguments to an object ```js var thenify = require('thenify'); var promise = thenify(function (callback) { callback(null, 1, 2, 3); }, { multiArgs: [ 'one', 'tow', 'three' ] }); // promise().then(function onFulfilled(value) { // assert.deepEqual(value, { // one: 1, // tow: 2, // three: 3 // }); // }); ``` [gitter-image]: https://badges.gitter.im/thenables/thenify.png [gitter-url]: https://gitter.im/thenables/thenify [npm-image]: https://img.shields.io/npm/v/thenify.svg?style=flat-square [npm-url]: https://npmjs.org/package/thenify [github-tag]: http://img.shields.io/github/tag/thenables/thenify.svg?style=flat-square [github-url]: https://github.com/thenables/thenify/tags [travis-image]: https://img.shields.io/travis/thenables/thenify.svg?style=flat-square [travis-url]: https://travis-ci.org/thenables/thenify [coveralls-image]: https://img.shields.io/coveralls/thenables/thenify.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/thenables/thenify [david-image]: http://img.shields.io/david/thenables/thenify.svg?style=flat-square [david-url]: https://david-dm.org/thenables/thenify [license-image]: http://img.shields.io/npm/l/thenify.svg?style=flat-square [license-url]: LICENSE [downloads-image]: http://img.shields.io/npm/dm/thenify.svg?style=flat-square [downloads-url]: https://npmjs.org/package/thenify thenify-3.3.0/index.js000066400000000000000000000042761310754066300146460ustar00rootroot00000000000000 var Promise = require('any-promise') var assert = require('assert') module.exports = thenify /** * Turn async functions into promises * * @param {Function} $$__fn__$$ * @return {Function} * @api public */ function thenify($$__fn__$$, options) { assert(typeof $$__fn__$$ === 'function') return eval(createWrapper($$__fn__$$.name, options)) } /** * Turn async functions into promises and backward compatible with callback * * @param {Function} $$__fn__$$ * @return {Function} * @api public */ thenify.withCallback = function ($$__fn__$$, options) { assert(typeof $$__fn__$$ === 'function') options = options || {} options.withCallback = true if (options.multiArgs === undefined) options.multiArgs = true return eval(createWrapper($$__fn__$$.name, options)) } function createCallback(resolve, reject, multiArgs) { return function(err, value) { if (err) return reject(err) var length = arguments.length if (length <= 2 || !multiArgs) return resolve(value) if (Array.isArray(multiArgs)) { var values = {} for (var i = 1; i < length; i++) values[multiArgs[i - 1]] = arguments[i] return resolve(values) } var values = new Array(length - 1) for (var i = 1; i < length; ++i) values[i - 1] = arguments[i] resolve(values) } } function createWrapper(name, options) { name = (name || '').replace(/\s|bound(?!$)/g, '') options = options || {} // default to true var multiArgs = options.multiArgs !== undefined ? options.multiArgs : true multiArgs = 'var multiArgs = ' + JSON.stringify(multiArgs) + '\n' var withCallback = options.withCallback ? 'var lastType = typeof arguments[len - 1]\n' + 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n' : '' return '(function ' + name + '() {\n' + 'var self = this\n' + 'var len = arguments.length\n' + multiArgs + withCallback + 'var args = new Array(len + 1)\n' + 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n' + 'var lastIndex = i\n' + 'return new Promise(function (resolve, reject) {\n' + 'args[lastIndex] = createCallback(resolve, reject, multiArgs)\n' + '$$__fn__$$.apply(self, args)\n' + '})\n' + '})' } thenify-3.3.0/package.json000066400000000000000000000013601310754066300154560ustar00rootroot00000000000000{ "name": "thenify", "description": "Promisify a callback-based function", "version": "3.3.0", "author": "Jonathan Ong (http://jongleberry.com)", "license": "MIT", "repository": "thenables/thenify", "dependencies": { "any-promise": "^1.0.0" }, "devDependencies": { "bluebird": "^3.1.1", "istanbul": "^0.4.0", "mocha": "^3.0.2" }, "scripts": { "test": "mocha --reporter spec", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot" }, "keywords": [ "promisify", "promise", "thenify", "then", "es6" ], "files": [ "index.js" ] } thenify-3.3.0/test/000077500000000000000000000000001310754066300141475ustar00rootroot00000000000000thenify-3.3.0/test/options.js000066400000000000000000000023701310754066300162020ustar00rootroot00000000000000 var assert = require('assert') var promisify = require('..') var setImmediate = global.setImmediate || function (fn) { process.nextTick(fn) } describe('options', function () { describe('.withCallback', function () { function fn(cb) { cb(null, true) } it('promise', function () { return promisify(fn, { withCallback: true })().then(function (val) { assert.equal(val, true) }) }) it('callback', function (done) { return promisify(fn, { withCallback: true })(function (err, val) { assert.equal(val, true) done() }) }) }) describe('.multiArgs', function () { function fn(cb) { cb(null, 1, 2, 3) } it('default to true', function () { return promisify(fn)().then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) it('set to false', function () { return promisify(fn, { multiArgs: false })().then(function (value) { assert.equal(value, 1) }) }) it('set to array', function () { return promisify(fn, { multiArgs: [ 'one', 'tow', 'three' ] })().then(function (value) { assert.deepEqual(value, { one: 1, tow: 2, three: 3 }) }) }) }) }) thenify-3.3.0/test/promisify.js000066400000000000000000000022651310754066300165330ustar00rootroot00000000000000 var assert = require('assert') var promisify = require('..') var setImmediate = global.setImmediate || function (fn) { process.nextTick(fn) } describe('Promisify', function () { it('should reject errors', function (done) { function fn(done) { setImmediate(function () { var err = new Error('boom') err.boom = 'boom' done(err) }) } var prom = promisify(fn) prom().catch(function (err) { assert.equal(err.message, 'boom') assert.equal(err.boom, 'boom') done() }) }) it('should reject errors with arguments', function (done) { function fn(a, b, done) { setImmediate(function () { done(new Error('boom')) }) } var prom = promisify(fn) prom(1, 2).catch(function (err) { assert.equal(err.message, 'boom') done() }) }) /** * bluebird can resolve multiple arguments, * but v8 promises can't. -_- */ it('should return the result', function (done) { function fn(done) { setImmediate(function () { done(null, 1) }) } var prom = promisify(fn) prom().then(function (a) { assert.equal(a, 1) done() }) }) }) thenify-3.3.0/test/test.js000066400000000000000000000031731310754066300154700ustar00rootroot00000000000000 var assert = require('assert') var thenify = require('..') it('fn.name', function () { function someCrazyName() {} assert.equal('someCrazyName', thenify(someCrazyName).name) assert.equal('someCrazyName', thenify(someCrazyName).name) // In ES6 spec, functions can infer the name of an anonymous function from its syntactic position. var noname = function () {} var name = noname.name assert.equal(name, thenify(noname).name) assert.equal(name, thenify.withCallback(noname).name) }) it('fn.name (bound function)', function () { function bound() {} assert.equal('bound', thenify(bound).name) assert.equal('bound', thenify.withCallback(bound).name) var noname = (function () {}).bind(this) assert.equal('', thenify(noname).name) assert.equal('', thenify.withCallback(noname).name) }) it('fn(callback(err))', function () { function fn(cb) { setTimeout(function () { cb(new Error('boom')) }, 0) } return thenify(fn)().then(function () { throw new Error('bang') }).catch(function (err) { assert.equal(err.message, 'boom') }) }) it('fn(callback(null, value))', function () { function fn(cb) { cb(null, true) } return thenify(fn)().then(function (val) { assert.equal(val, true) }) }) it('fn(callback(null, values...))', function () { function fn(cb) { cb(null, 1, 2, 3) } return thenify(fn)().then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) it('fn(..args, callback())', function () { function fn(a, b, c, cb) { cb(null, a, b, c) } return thenify(fn)(1, 2, 3).then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) thenify-3.3.0/test/withcallback.js000066400000000000000000000044251310754066300171420ustar00rootroot00000000000000var assert = require('assert') var promisify = require('../').withCallback describe('with callback backward compatible', function () { describe('fn.name', function () { function someCrazyName() {} it('should set function name by default', function () { assert.equal('someCrazyName', promisify(someCrazyName).name) }) it('should set name to empty', function () { assert.equal('', promisify(function() {}).name) }) }) describe('fn(callback(err)', function () { function fn(cb) { setTimeout(function () { cb(new Error('boom')) }, 0) } it('promise', function () { return promisify(fn)().then(function () { throw new Error('bang') }).catch(function (err) { assert.equal(err.message, 'boom') }) }) it('callback', function (done) { promisify(fn)(function (err) { assert.equal(err.message, 'boom') done() }) }) }) describe('fn(callback(null, value))', function () { function fn(cb) { cb(null, true) } it('promise', function () { return promisify(fn)().then(function (val) { assert.equal(val, true) }) }) it('callback', function (done) { return promisify(fn)(function (err, val) { assert.equal(val, true) done() }) }) }) describe('fn(callback(null, values...))', function () { function fn(cb) { cb(null, 1, 2, 3) } it('promise', function () { return promisify(fn)().then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) it('callback', function (done) { promisify(fn)(function (err, val1, val2, val3) { assert.equal(val1, 1) assert.equal(val2, 2) assert.equal(val3, 3) done() }) }) }) describe('fn(..args, callback())', function () { function fn(a, b, c, cb) { cb(null, a, b, c) } it('promise', function () { return promisify(fn)(1, 2, 3).then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) it('callback', function (done) { promisify(fn)(1, 2, 3, function (err, val1, val2, val3) { assert.equal(val1, 1) assert.equal(val2, 2) assert.equal(val3, 3) done() }) }) }) })