summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2015-07-19 23:17:30 -0700
committerAlexander Early <alexander.early@gmail.com>2015-07-19 23:17:30 -0700
commit4101af3f402008ea82c8de38ec3bd852aa3d7565 (patch)
treea2cec5307d72c493af80b15ccf2d1863bee4dc77
parent2d00945606341facc1731723310fe32f6cfbc502 (diff)
parent55c5ac953674c77c776a13c77a0d3d180098be92 (diff)
downloadasync-4101af3f402008ea82c8de38ec3bd852aa3d7565.tar.gz
Merge pull request #844 from megawac/types
Stricter type checking
-rw-r--r--lib/async.js14
-rwxr-xr-xtest/test-async.js11
2 files changed, 21 insertions, 4 deletions
diff --git a/lib/async.js b/lib/async.js
index f80dbcf..9c2b6af 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -62,6 +62,12 @@
return _toString.call(obj) === '[object Array]';
};
+ // Ported from underscore.js isObject
+ var _isObject = function(obj) {
+ var type = typeof obj;
+ return type === 'function' || type === 'object' && !!obj;
+ };
+
function _isArrayLike(arr) {
return _isArray(arr) || (
// has a positive integer length property
@@ -1012,7 +1018,7 @@
function _console_fn(name) {
return _restParam(function (fn, args) {
fn.apply(null, args.concat([_restParam(function (err, args) {
- if (typeof console !== 'undefined') {
+ if (typeof console === 'object') {
if (err) {
if (console.error) {
console.error(err);
@@ -1185,7 +1191,7 @@
return callback(e);
}
// if result is Promise object
- if (typeof result !== 'undefined' && typeof result.then === "function") {
+ if (_isObject(result) && typeof result.then === "function") {
result.then(function(value) {
callback(null, value);
}).catch(function(err) {
@@ -1198,11 +1204,11 @@
};
// Node.js
- if (typeof module !== 'undefined' && module.exports) {
+ if (typeof module === 'object' && module.exports) {
module.exports = async;
}
// AMD / RequireJS
- else if (typeof define !== 'undefined' && define.amd) {
+ else if (typeof define === 'function' && define.amd) {
define([], function () {
return async;
});
diff --git a/test/test-async.js b/test/test-async.js
index 72ef132..8a7ea8d 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -4311,6 +4311,17 @@ exports['asyncify'] = {
});
},
+ 'asyncify null': function (test) {
+ var parse = async.asyncify(function() {
+ return null;
+ });
+ parse("{\"a\":1}", function (err, result) {
+ test.ok(!err);
+ test.ok(result === null);
+ test.done();
+ });
+ },
+
'variable numbers of arguments': function (test) {
async.asyncify(function (x, y, z) {
test.ok(arguments.length === 3);