This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 4 files changed +38
-5
lines changed
4 files changed +38
-5
lines changed Original file line number Diff line number Diff line change @@ -674,7 +674,7 @@ function isBoolean(value) {
674
674
675
675
676
676
function isPromiseLike ( obj ) {
677
- return obj && isFunction ( obj . then ) ;
677
+ return ( isObject ( obj ) || isFunction ( obj ) ) && isFunction ( obj . then ) ;
678
678
}
679
679
680
680
Original file line number Diff line number Diff line change @@ -561,6 +561,22 @@ function qFactory(nextTick, exceptionHandler) {
561
561
return deferred . promise ;
562
562
}
563
563
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
+
564
580
var $Q = function Q ( resolver ) {
565
581
if ( ! isFunction ( resolver ) ) {
566
582
throw $qMinErr ( 'norslvr' , "Expected resolverFn, got '{0}'" , resolver ) ;
@@ -590,6 +606,7 @@ function qFactory(nextTick, exceptionHandler) {
590
606
$Q . when = when ;
591
607
$Q . resolve = resolve ;
592
608
$Q . all = all ;
609
+ $Q . isPromiseLike = isPromiseLike ;
593
610
594
611
return $Q ;
595
612
}
Original file line number Diff line number Diff line change @@ -69,10 +69,6 @@ var ANIMATION_DURATION_PROP = ANIMATION_PROP + DURATION_KEY;
69
69
var TRANSITION_DELAY_PROP = TRANSITION_PROP + DELAY_KEY ;
70
70
var TRANSITION_DURATION_PROP = TRANSITION_PROP + DURATION_KEY ;
71
71
72
- var isPromiseLike = function ( p ) {
73
- return p && p . then ? true : false ;
74
- } ;
75
-
76
72
var ngMinErr = angular . $$minErr ( 'ng' ) ;
77
73
function assertArg ( arg , name , reason ) {
78
74
if ( ! arg ) {
Original file line number Diff line number Diff line change @@ -1972,6 +1972,26 @@ describe('q', function() {
1972
1972
} ) ;
1973
1973
} ) ;
1974
1974
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
+
1975
1995
describe ( 'exception logging' , function ( ) {
1976
1996
var mockExceptionLogger = {
1977
1997
log : [ ] ,
You can’t perform that action at this time.
0 commit comments