Skip to content

Commit

Permalink
Merge pull request #840 from zartdinov/master
Browse files Browse the repository at this point in the history
asyncify promisified functions
  • Loading branch information
aearly committed Jul 9, 2015
2 parents 7ea45d7 + 97f9825 commit e82ce4b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,16 @@
} catch (e) {
return callback(e);
}
callback(null, result);
// if result is Promise object
if (typeof result !== 'undefined' && typeof result.then === "function") {
result.then(function(value) {
callback(null, value);
}).catch(function(err) {
callback(err.message ? err : new Error(err));
});
} else {
callback(null, result);
}
});
};

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
"license": "MIT",
"devDependencies": {
"benchmark": "bestiejs/benchmark.js",
"bluebird": "^2.9.32",
"coveralls": "^2.11.2",
"es6-promise": "^2.3.0",
"jscs": "^1.13.1",
"jshint": "~2.8.0",
"lodash": "^3.9.0",
"mkdirp": "~0.5.1",
"native-promise-only": "^0.8.0-a",
"nodeunit": ">0.0.0",
"nyc": "^2.1.0",
"rsvp": "^3.0.18",
"uglify-js": "~2.4.0",
"xyz": "^0.5.0",
"yargs": "~3.9.1"
Expand Down
50 changes: 49 additions & 1 deletion test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4267,5 +4267,53 @@ exports['asyncify'] = {
test.ok(e.message === "callback error");
test.done();
}
}
},

'promisified': [
'native-promise-only',
'bluebird',
'es6-promise',
'rsvp'
].reduce(function(promises, name) {
if (isBrowser()) {
// node only test
return;
}
var Promise = require(name);
if (typeof Promise.Promise === 'function') {
Promise = Promise.Promise;
}
promises[name] = {
'resolve': function(test) {
var promisified = function(argument) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(argument + " resolved");
}, 15);
});
};
async.asyncify(promisified)("argument", function (err, value) {
if (err) {
return test.done(new Error("should not get an error here"));
}
test.ok(value === "argument resolved");
test.done();
});
},

'reject': function(test) {
var promisified = function(argument) {
return new Promise(function (resolve, reject) {
reject(argument + " rejected");
});
};
async.asyncify(promisified)("argument", function (err) {
test.ok(err);
test.ok(err.message === "argument rejected");
test.done();
});
}
};
return promises;
}, {})
};

0 comments on commit e82ce4b

Please sign in to comment.