feat(facade): add support for all thenables (#10278)

All objects that have a then function will be considered Promises
This commit is contained in:
Gabe Johnson 2016-07-27 12:37:48 -05:00 committed by Victor Berchet
parent 9d9e9c6ff1
commit 58d9e7fc5a
2 changed files with 22 additions and 2 deletions

View File

@ -133,7 +133,9 @@ export function isStrictStringMap(obj: any): boolean {
}
export function isPromise(obj: any): boolean {
return obj instanceof (<any>_global).Promise;
// allow any Promise/A+ compliant thenable.
// It's up to the caller to ensure that obj.then conforms to the spec
return isPresent(obj) && isFunction(obj.then);
}
export function isArray(obj: any): boolean {

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {NumberWrapper, RegExpMatcherWrapper, RegExpWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, resolveEnumToken} from '../src/lang';
import {NumberWrapper, RegExpMatcherWrapper, RegExpWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, isPromise, resolveEnumToken} from '../src/lang';
enum UsefulEnum {
MyToken,
@ -184,4 +184,22 @@ export function main() {
() => { expect(hasConstructor(new MySubclass(), MySuperclass)).toEqual(false); });
});
});
describe('isPromise', () => {
it('should be true for native Promises',
() => expect(isPromise(Promise.resolve(true))).toEqual(true));
it('should be true for thenables',
() => expect(isPromise({then: function() {}})).toEqual(true));
it('should be false if "then" is not a function',
() => expect(isPromise({then: 0})).toEqual(false));
it('should be false if the argument has no "then" function',
() => expect(isPromise({})).toEqual(false));
it('should be false if the argument is undefined or null', () => {
expect(isPromise(undefined)).toEqual(false);
expect(isPromise(null)).toEqual(false);
});
});
}