{"_id":"promise-resolver","_rev":"13086","name":"promise-resolver","description":"Provide flexible methods that accept callbacks and return promises without requiring a Promise implementation exist","dist-tags":{"latest":"3.0.0"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"time":{"modified":"2021-06-03T09:58:20.000Z","created":"2015-10-06T21:59:19.030Z","3.0.0":"2015-10-19T05:50:32.395Z","2.0.0":"2015-10-18T04:53:56.403Z","1.1.0":"2015-10-14T01:27:41.335Z","1.0.0":"2015-10-13T21:26:36.819Z","0.1.0":"2015-10-06T21:59:19.030Z"},"users":{},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"versions":{"3.0.0":{"name":"promise-resolver","version":"3.0.0","description":"Provide flexible methods that accept callbacks and return promises without requiring a Promise implementation exist","license":"MIT","repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"engines":{"node":">=0.8.0"},"scripts":{"pretest":"(node-version --gte-0.12.0 && npm rm bluebird) || npm install bluebird","test":"(node-version --lt-0.10.0 || xo) && mocha $(node-version --lt-1.0.0 --compilers js:babel/register)"},"files":["index.js"],"keywords":["promise","promises","promisification","promisify","callback","callbacks","nodeback","node"],"dependencies":{"native-or-bluebird":"^1.2.0","safecb":"0.0.1"},"devDependencies":{"babel":"^5.8.23","co-mocha":"^1.1.2","mocha":"^2.2.5","node-version-cli":"^1.1.4","proxyquire":"^1.7.3","sinon":"^1.17.1","xo":"^0.9.0"},"xo":{"envs":["node","mocha"]},"gitHead":"f7343d0efcae799a6c2d143dd6ba127703bb7895","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","_id":"promise-resolver@3.0.0","_shasum":"3695009cffb8a32ac2ebdc8d22be52a798740626","_from":".","_npmVersion":"2.14.4","_nodeVersion":"4.1.2","_npmUser":{"name":"jamestalmage","email":"james@talmage.io"},"dist":{"shasum":"3695009cffb8a32ac2ebdc8d22be52a798740626","size":3546,"noattachment":false,"key":"/promise-resolver/-/promise-resolver-3.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/promise-resolver/download/promise-resolver-3.0.0.tgz"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"directories":{},"publish_time":1445233832395,"_cnpm_publish_time":1445233832395,"_hasShrinkwrap":false},"2.0.0":{"name":"promise-resolver","version":"2.0.0","description":"Turn a promises resolver methods into a node style callback","license":"MIT","repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"engines":{"node":">=0.8.0"},"scripts":{"pretest":"(node-version --gte-0.12.0 && npm rm bluebird) || npm install bluebird","test":"(node-version --lt-0.10.0 || xo) && mocha"},"files":["index.js"],"keywords":["promise","promises","promisification","promisify","callback","callbacks","nodeback","node"],"dependencies":{"native-or-bluebird":"^1.2.0"},"devDependencies":{"mocha":"^2.2.5","node-version-cli":"^1.1.4","sinon":"^1.17.1","xo":"^0.9.0"},"xo":{"envs":["node","mocha"]},"gitHead":"7ecf76ffdeaf252c86ffef7c41e51b9466e7fc20","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","_id":"promise-resolver@2.0.0","_shasum":"68a273bd0d05af3b7c1521c9a8bbc4ad45728aca","_from":".","_npmVersion":"2.14.4","_nodeVersion":"4.1.2","_npmUser":{"name":"jamestalmage","email":"james@talmage.io"},"dist":{"shasum":"68a273bd0d05af3b7c1521c9a8bbc4ad45728aca","size":2630,"noattachment":false,"key":"/promise-resolver/-/promise-resolver-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/promise-resolver/download/promise-resolver-2.0.0.tgz"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"directories":{},"publish_time":1445144036403,"_cnpm_publish_time":1445144036403,"_hasShrinkwrap":false},"1.1.0":{"name":"promise-resolver","version":"1.1.0","description":"Turn a promises resolver methods into a node style callback","license":"MIT","repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"engines":{"node":">=0.8.0"},"scripts":{"test":"(node-version --lt-0.10.0 || xo) && mocha"},"files":["index.js"],"keywords":["promise","promises","promisification","promisify","callback","callbacks","nodeback","node"],"dependencies":{},"devDependencies":{"mocha":"^2.2.5","node-version-cli":"^1.1.4","sinon":"^1.17.1","xo":"^0.9.0"},"xo":{"envs":["node","mocha"]},"gitHead":"101a965e673b3f6ad70d485341120a39cbfd1de3","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","_id":"promise-resolver@1.1.0","_shasum":"08c9cd6463ebd0c1bb391898efc4ebdec0a41fb8","_from":".","_npmVersion":"2.14.4","_nodeVersion":"4.1.2","_npmUser":{"name":"jamestalmage","email":"james@talmage.io"},"dist":{"shasum":"08c9cd6463ebd0c1bb391898efc4ebdec0a41fb8","size":2103,"noattachment":false,"key":"/promise-resolver/-/promise-resolver-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/promise-resolver/download/promise-resolver-1.1.0.tgz"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"directories":{},"publish_time":1444786061335,"_cnpm_publish_time":1444786061335,"_hasShrinkwrap":false},"1.0.0":{"name":"promise-resolver","version":"1.0.0","description":"Turn a promises resolver methods into a node style callback","license":"MIT","repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"engines":{"node":">=0.10.0"},"scripts":{"test":"xo && mocha"},"files":["index.js"],"keywords":["promise","promises","promisification","promisify","callback","callbacks","nodeback","node"],"dependencies":{},"devDependencies":{"mocha":"^2.2.5","sinon":"^1.17.1","xo":"^0.9.0"},"xo":{"envs":["node","mocha"]},"gitHead":"6c3a4b34f834699241b99b528f98ce52470d55fc","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","_id":"promise-resolver@1.0.0","_shasum":"78acec6d84bf60ae7ec28e2ec7224d6a11ed22e8","_from":".","_npmVersion":"2.14.2","_nodeVersion":"0.12.7","_npmUser":{"name":"jamestalmage","email":"james@talmage.io"},"dist":{"shasum":"78acec6d84bf60ae7ec28e2ec7224d6a11ed22e8","size":1767,"noattachment":false,"key":"/promise-resolver/-/promise-resolver-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/promise-resolver/download/promise-resolver-1.0.0.tgz"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"directories":{},"publish_time":1444771596819,"_cnpm_publish_time":1444771596819,"_hasShrinkwrap":false},"0.1.0":{"name":"promise-resolver","version":"0.1.0","description":"Turn a promises resolver methods into a node style callback","license":"MIT","repository":{"type":"git","url":"git+https://github.com/jamestalmage/promise-resolver.git"},"author":{"name":"James Talmage","email":"james@talmage.io","url":"github.com/jamestalmage"},"engines":{"node":">=0.10.0"},"scripts":{"test":"xo && mocha"},"files":["index.js"],"keywords":["promise","promises","promisification","promisify","callback","callbacks","nodeback","node"],"dependencies":{},"devDependencies":{"mocha":"^2.2.5","sinon":"^1.17.1","xo":"^0.9.0"},"xo":{"envs":["node","mocha"]},"gitHead":"c61ff83c5f6ce379ac3dddd20d897c05d882b422","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","_id":"promise-resolver@0.1.0","_shasum":"590a530d064b7d36e11edd6df395dc0a610bcf99","_from":".","_npmVersion":"2.14.2","_nodeVersion":"0.12.7","_npmUser":{"name":"jamestalmage","email":"james@talmage.io"},"dist":{"shasum":"590a530d064b7d36e11edd6df395dc0a610bcf99","size":1579,"noattachment":false,"key":"/promise-resolver/-/promise-resolver-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/promise-resolver/download/promise-resolver-0.1.0.tgz"},"maintainers":[{"name":"jamestalmage","email":"james@talmage.io"}],"directories":{},"publish_time":1444168759030,"_cnpm_publish_time":1444168759030,"_hasShrinkwrap":false}},"readme":"# promise-resolver [![Build Status](https://travis-ci.org/jamestalmage/promise-resolver.svg?branch=master)](https://travis-ci.org/jamestalmage/promise-resolver)\n\n> Provide flexible methods that accept callbacks and return promises without requiring a Promise implementation exist\n\n\n## Install\n\n```\n$ npm install --save promise-resolver\n```\n\n\n## Usage\n\n```js\nvar promiseResolver = require('promise-resolver');\n\nfunction sayMyName(name, cb) {\n  var deferred = promiseResolver.defer(cb);\n  \n  // this resolves the promise and calls the callback (asynchronously).\n  deferred.cb(null, 'My name is ' + name + '!');\n  \n  return deferred.promise;\n```\n\n`sayMyName` can now be used in one of two ways:\n\n\n##### Provide a callback\n\n```js\nsayMyName('James', function (error, message) {\n  console.log(message);\n  // => 'My name is James!'\n});\n```\n\n\n##### Use the returned promise\n\n```js\nsayMyName('Susan').then(function (message) {\n  console.log(message);\n  // => 'My name is Susan!'\n});\n```\n\nIf you do not provide a callback, then you should `catch` errors on the promise. Most promise implementations emit\n [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection) events.\n If a callback is provided, \n [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection) \n events are be suppressed (it is assumed the callback handles any errors).\n \n \n#### Safe Callbacks\n\n`promise-resolver` protects against callback misuse in the following ways:\n\n```js\nfunction doStuff (cb) {\n  var deferred = promiseResolver(cb);\n  // prevent these typical problems:\n  deferred.cb() // synchronous invocation\n  deferred.cb() // multiple invocations\n  deferred.cb() // undefined is not a function (deferred.cb is always defined, even if cb is not)\n}\n```\n\n\n#### Missing Promise Implementation\n\n`promise-resolver` allows you to create API's that provide the convenience of Promises,\n without demanding a bulky Promise polyfill on systems that do not already have an implementation.\n It looks first for `bluebird` and then a native `Promise` implementation.\n\nIf the user does *not* supply a callback *and* no promise implementation is found, an\n error will be thrown explaining how to resolve the problem:\n\n```\n  No Promise Implementation: You must use a callback function, upgrade to Node >= 0.11.13, or install bluebird\n```\n\nIf it does *not* find a promise implementation, but a callback *is* found then it will still return a `deferred`, but \n `deferred.promise` will be `undefined`.\n \nFinally, `promiseResolver.defer(cb, Promise)` does allow you to specify an alternate Promise implementation as the second argument. \n\n\n## API\n \n### promiseResolver.defer(passThrough, Promise)\n\n* `passThrough` - a \"pass through\" node style callback as described above\n* `Promise` - an alternate Promise constructor (will use `bluebird` or native `Promise` implementation by default).\n\nThe return value is a standard `defer` object with an additional `cb` property\nthat is a node style resolver callback.\n \n```js\nvar deferred = promiseResolver(passThroughCallback);\n\n// rejects promise and calls passThroughCallback with same args\ndeferred.cb(new Error('...')); \n\n// resolves promise and calls passThroughCallback with same args\ndeferred. cb(null, 'result'); \n\nreturn deferred.promise;\n```\n\n* `deferred.cb` will resolve/reject the promise and call `passThroughCallback`\n* Ensures that `passThroughCallback` is only called once.\n* If `passThroughCallback` is provided, it is assumed to handle any errors, and so `unhandledRejection` errors on \n  the promise will be suppressed. This avoids potentially confusing console warnings if users are handling errors\n  via a callback and ignoring the returned promise. \n* `deferred.resolve` and `deferred.reject` are also available, and behave as expected.\n* `deferred.promise` will be `undefined` if no Promise implementation is found (in that case `passThroughCallback` is required).\n\n\n### promiseResolver(resolve, reject, passThrough) \n\nAll arguments should be functions, null, or undefined.\n\n* `resolve` - promise resolve function\n* `reject` - promise reject function\n* `passThrough` - a \"pass through\" node style (error first) callback.\n\nReturns a node style callback: `cb(err, result...)`\n\nCalling the callback will resolve or reject the promise (depending on the `err` argument).\nIf it exists, the `passThrough` callback will be called with the same arguments.\n\n```js\nvar promiseResolver = require('promise-resolver');\n\nreturn new Promise(function (resolve, reject) {\n  var cb = promiseResolver(resolve, reject, passThroughCallback);\n  \n  cb(new Error('...'));\n  \n  cb(null, 'result');\n});\n```\n\nThis behaves similar to the `defer` method, with the only exception being that `unhandledRejection` errors are *not* \nautomatically suppressed when `passThroughCallback` is supplied. It also requires you find and invoke the Promise implementation.\n\n\n## License\n\nMIT © [James Talmage](http://github.com/jamestalmage)\n","_attachments":{},"homepage":"https://github.com/jamestalmage/promise-resolver#readme","bugs":{"url":"https://github.com/jamestalmage/promise-resolver/issues"},"license":"MIT"}