Skip to content

Commit d1df21e

Browse files
shahatajeffbcross
authored andcommitted
refactor(Angular): add isPromiseLike helper function
This can be used internally to remove the repeating pattern of `obj && obj.then`. For now, I don't see a good reason to expose this in angular's public interface.
1 parent 4d875fa commit d1df21e

File tree

6 files changed

+12
-4
lines changed

6 files changed

+12
-4
lines changed

lib/promises-aplus/promises-aplus-test-adapter.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
var isFunction = function isFunction(value){return typeof value == 'function';};
5+
var isPromiseLike = function isPromiseLike(obj) {return obj && isFunction(obj.then);};
56

67
var $q = qFactory(process.nextTick, function noopExceptionHandler() {});
78

src/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"isFile": false,
5252
"isBlob": false,
5353
"isBoolean": false,
54+
"isPromiseLike": false,
5455
"trim": false,
5556
"isElement": false,
5657
"makeMap": false,

src/Angular.js

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
isFile: true,
4848
isBlob: true,
4949
isBoolean: true,
50+
isPromiseLike: true,
5051
trim: true,
5152
isElement: true,
5253
makeMap: true,
@@ -569,6 +570,11 @@ function isBoolean(value) {
569570
}
570571

571572

573+
function isPromiseLike(obj) {
574+
return obj && isFunction(obj.then);
575+
}
576+
577+
572578
var trim = (function() {
573579
// native trim is way faster: http://jsperf.com/angular-trim-test
574580
// but IE doesn't have it... :-(

src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ function $HttpProvider() {
895895
if (cache) {
896896
cachedResp = cache.get(url);
897897
if (isDefined(cachedResp)) {
898-
if (cachedResp.then) {
898+
if (isPromiseLike(cachedResp)) {
899899
// cached request has already been sent, but there is no response yet
900900
cachedResp.then(removePendingReq, removePendingReq);
901901
return cachedResp;

src/ng/httpBackend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
132132

133133
if (timeout > 0) {
134134
var timeoutId = $browserDefer(timeoutRequest, timeout);
135-
} else if (timeout && timeout.then) {
135+
} else if (isPromiseLike(timeout)) {
136136
timeout.then(timeoutRequest);
137137
}
138138

src/ng/q.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function qFactory(nextTick, exceptionHandler) {
310310
} catch(e) {
311311
return makePromise(e, false);
312312
}
313-
if (callbackOutput && isFunction(callbackOutput.then)) {
313+
if (isPromiseLike(callbackOutput)) {
314314
return callbackOutput.then(function() {
315315
return makePromise(value, isResolved);
316316
}, function(error) {
@@ -335,7 +335,7 @@ function qFactory(nextTick, exceptionHandler) {
335335

336336

337337
var ref = function(value) {
338-
if (value && isFunction(value.then)) return value;
338+
if (isPromiseLike(value)) return value;
339339
return {
340340
then: function(callback) {
341341
var result = defer();

0 commit comments

Comments
 (0)