Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f47fbcf

Browse files
committed
feat(q): add isPromiseLike
- Add `isPromiseLike` public method for determining whether value is a promise-like object
1 parent 798fb18 commit f47fbcf

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/Angular.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ function isBoolean(value) {
674674

675675

676676
function isPromiseLike(obj) {
677-
return obj && isFunction(obj.then);
677+
return (isObject(obj) || isFunction(obj)) && isFunction(obj.then);
678678
}
679679

680680

src/ng/q.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,22 @@ function qFactory(nextTick, exceptionHandler) {
561561
return deferred.promise;
562562
}
563563

564+
/**
565+
* @ngdoc method
566+
* @name $q#isPromiseLike
567+
* @kind function
568+
*
569+
* @description
570+
* Determines whether object or function is like a promise
571+
*
572+
* @param {*} value Reference to check
573+
* @returns {boolean} True if `value` is promise-like
574+
*/
575+
576+
function isPromiseLike(obj) {
577+
return (isObject(obj) || isFunction(obj)) && isFunction(obj.then);
578+
}
579+
564580
var $Q = function Q(resolver) {
565581
if (!isFunction(resolver)) {
566582
throw $qMinErr('norslvr', "Expected resolverFn, got '{0}'", resolver);
@@ -590,6 +606,7 @@ function qFactory(nextTick, exceptionHandler) {
590606
$Q.when = when;
591607
$Q.resolve = resolve;
592608
$Q.all = all;
609+
$Q.isPromiseLike = isPromiseLike;
593610

594611
return $Q;
595612
}

src/ngAnimate/shared.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ var ANIMATION_DURATION_PROP = ANIMATION_PROP + DURATION_KEY;
6969
var TRANSITION_DELAY_PROP = TRANSITION_PROP + DELAY_KEY;
7070
var TRANSITION_DURATION_PROP = TRANSITION_PROP + DURATION_KEY;
7171

72-
var isPromiseLike = function(p) {
73-
return p && p.then ? true : false;
74-
};
75-
7672
var ngMinErr = angular.$$minErr('ng');
7773
function assertArg(arg, name, reason) {
7874
if (!arg) {

test/ng/qSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,26 @@ describe('q', function() {
19721972
});
19731973
});
19741974

1975+
describe('isPromiseLike', function() {
1976+
it('should return false if not returned an object with then method', function() {
1977+
expect(q.isPromiseLike(1)).toBe(false);
1978+
expect(q.isPromiseLike('foo')).toBe(false);
1979+
expect(q.isPromiseLike({})).toBe(false);
1980+
expect(q.isPromiseLike(function() {})).toBe(false);
1981+
});
1982+
1983+
it('should return true if passed an object with then method', function() {
1984+
expect(q.isPromiseLike({then: angular.noop})).toBe(true);
1985+
});
1986+
1987+
it('should return true if passed a function with then method', function() {
1988+
function foo() {}
1989+
foo.then = angular.noop;
1990+
1991+
expect(q.isPromiseLike(foo)).toBe(true);
1992+
});
1993+
});
1994+
19751995
describe('exception logging', function() {
19761996
var mockExceptionLogger = {
19771997
log: [],

0 commit comments

Comments
 (0)