pax_global_header00006660000000000000000000000064136724514720014525gustar00rootroot0000000000000052 comment=1d054b4d60e72087f4e804565e540258ecee7f0b thenify-3.3.1/000077500000000000000000000000001367245147200131775ustar00rootroot00000000000000thenify-3.3.1/.gitignore000066400000000000000000000000561367245147200151700ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage thenify-3.3.1/.travis.yml000066400000000000000000000002341367245147200153070ustar00rootroot00000000000000node_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.1/History.md000066400000000000000000000004751367245147200151700ustar00rootroot00000000000000 3.3.1 / 2020-06-18 ================== **fixes** * [[`0d94a24`](http://github.com/thenables/thenify/commit/0d94a24eb933bc835d568f3009f4d269c4c4c17a)] - fix: remove eval (#30) (Yiyu He <>) 3.3.0 / 2017-05-19 ================== * feat: support options.multiArgs and options.withCallback (#27) thenify-3.3.1/LICENSE000066400000000000000000000021411367245147200142020ustar00rootroot00000000000000 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.1/README.md000066400000000000000000000073101367245147200144570ustar00rootroot00000000000000 # 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.1/index.js000066400000000000000000000036321367245147200146500ustar00rootroot00000000000000 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 createWrapper(fn, 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 return createWrapper(fn, options) } function createCallback(resolve, reject, multiArgs) { // default to true if (multiArgs === undefined) multiArgs = true 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(fn, options) { options = options || {} var name = fn.name; name = (name || '').replace(/\s|bound(?!$)/g, '') var newFn = function () { var self = this var len = arguments.length if (options.withCallback) { var lastType = typeof arguments[len - 1] if (lastType === 'function') return fn.apply(self, arguments) } var args = new Array(len + 1) for (var i = 0; i < len; ++i) args[i] = arguments[i] var lastIndex = i return new Promise(function (resolve, reject) { args[lastIndex] = createCallback(resolve, reject, options.multiArgs) fn.apply(self, args) }) } Object.defineProperty(newFn, 'name', { value: name }) return newFn } thenify-3.3.1/package.json000066400000000000000000000013601367245147200154650ustar00rootroot00000000000000{ "name": "thenify", "description": "Promisify a callback-based function", "version": "3.3.1", "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.1/test/000077500000000000000000000000001367245147200141565ustar00rootroot00000000000000thenify-3.3.1/test/options.js000066400000000000000000000023701367245147200162110ustar00rootroot00000000000000 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.1/test/promisify.js000066400000000000000000000022651367245147200165420ustar00rootroot00000000000000 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.1/test/test.js000066400000000000000000000043641367245147200155020ustar00rootroot00000000000000 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]) }) }) it('unicode function name', function () { function 你好$hello_123(a, b, c, cb) { cb(null, a, b, c) } var wrapper = thenify(你好$hello_123) assert.equal(wrapper.name, '你好$hello_123') wrapper(1, 2, 3).then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) it('invalid function name', function () { function fn(a, b, c, cb) { cb(null, a, b, c) } Object.defineProperty(fn, 'name', { value: 'fake(){a.b;})();(function(){//' }) var wrapper = thenify(fn) assert.equal(wrapper.name, fn.name) wrapper(1, 2, 3).then(function (values) { assert.deepEqual(values, [1, 2, 3]) }) }) thenify-3.3.1/test/withcallback.js000066400000000000000000000044251367245147200171510ustar00rootroot00000000000000var 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() }) }) }) })