Skip to content

Commit

Permalink
Added string detection in isPromise()
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxy committed Mar 28, 2023
1 parent bf6dc45 commit dc8225b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/router/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@ exports.isAsyncFunc = function(value) {
};

/**
* Check if object is Promise.
* Check if object is (or returns) Promise.
*
* @param {Object} obj
* Promise object.
*
* @return {Boolean}
*
* @example
* const result = isPromise(new Promise(() => {}));
* const promise = new Promise(() => {});
* const result = isPromise(promise);
* // true
*
* const result = function() {
* return promise;
* };
* // true
*/
exports.isPromise = function(obj) {
return (obj && (obj[Symbol.toStringTag] === 'Promise' || typeof obj.then === 'function'));
return (obj && (obj[Symbol.toStringTag] === 'Promise' || typeof obj.then === 'function' || /new Promise/.test(obj.toString())));
};

/**
Expand Down
3 changes: 3 additions & 0 deletions test/unit/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('Common module', function() {
const result1 = Common.isPromise(new Promise(() => {}));
const result2 = Common.isPromise(new Object());
const result3 = Common.isPromise(new Function());
const result4 = Common.isPromise(function() {
return new Promise(() => {});
});

it('should return value', function() {
expect(result1).to.be.true;
Expand Down

0 comments on commit dc8225b

Please sign in to comment.