|
|
1 nedēļu atpakaļ | |
|---|---|---|
| .. | ||
| History.md | 1 nedēļu atpakaļ | |
| LICENSE | 1 nedēļu atpakaļ | |
| README.md | 1 nedēļu atpakaļ | |
| index.js | 1 nedēļu atpakaļ | |
| package.json | 1 nedēļu atpakaļ | |
Promisify a callback-based function using any-promise.
bluebirdArray, also support change the behavior by options.multiArgsAn added benefit is that thrown errors in that async function will be caught by the promise!
Promisifies a function.
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 arrayfalse- always use the first argumentArray - converts multiple arguments to an object with keys provided in options.multiArgsTurn async functions into promises
var thenify = require('thenify');
var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
callback(null, a, b, c);
});
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()
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 () {});
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);
// });
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
// });
// });