Skip to content

Commit e6ebfc8

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. Conflicts: src/Angular.js
1 parent 7f2bcc3 commit e6ebfc8

File tree

6 files changed

+12
-4
lines changed

6 files changed

+12
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"isFile": false,
5151
"isBlob": false,
5252
"isBoolean": false,
53+
"isPromiseLike": false,
5354
"trim": false,
5455
"isElement": false,
5556
"makeMap": false,

src/Angular.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
isFile: true,
4747
isBlob: true,
4848
isBoolean: true,
49+
isPromiseLike: true,
4950
trim: true,
5051
isElement: true,
5152
makeMap: true,
@@ -582,6 +583,11 @@ function isBoolean(value) {
582583
}
583584

584585

586+
function isPromiseLike(obj) {
587+
return obj && isFunction(obj.then);
588+
}
589+
590+
585591
var trim = (function() {
586592
// native trim is way faster: http://jsperf.com/angular-trim-test
587593
// but IE doesn't have it... :-(

src/ng/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ function $HttpProvider() {
954954
if (cache) {
955955
cachedResp = cache.get(url);
956956
if (isDefined(cachedResp)) {
957-
if (cachedResp.then) {
957+
if (isPromiseLike(cachedResp)) {
958958
// cached request has already been sent, but there is no response yet
959959
cachedResp.then(removePendingReq, removePendingReq);
960960
return cachedResp;

src/ng/httpBackend.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function qFactory(nextTick, exceptionHandler) {
303303
} catch(e) {
304304
return makePromise(e, false);
305305
}
306-
if (callbackOutput && isFunction(callbackOutput.then)) {
306+
if (isPromiseLike(callbackOutput)) {
307307
return callbackOutput.then(function() {
308308
return makePromise(value, isResolved);
309309
}, function(error) {
@@ -328,7 +328,7 @@ function qFactory(nextTick, exceptionHandler) {
328328

329329

330330
var ref = function(value) {
331-
if (value && isFunction(value.then)) return value;
331+
if (isPromiseLike(value)) return value;
332332
return {
333333
then: function(callback) {
334334
var result = defer();

0 commit comments

Comments
 (0)