mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-21 10:05:34 +08:00
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
This commit is contained in:
@@ -50,6 +50,7 @@
|
||||
"isFile": false,
|
||||
"isBlob": false,
|
||||
"isBoolean": false,
|
||||
"isPromiseLike": false,
|
||||
"trim": false,
|
||||
"isElement": false,
|
||||
"makeMap": false,
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
isFile: true,
|
||||
isBlob: true,
|
||||
isBoolean: true,
|
||||
isPromiseLike: true,
|
||||
trim: true,
|
||||
isElement: true,
|
||||
makeMap: true,
|
||||
@@ -582,6 +583,11 @@ function isBoolean(value) {
|
||||
}
|
||||
|
||||
|
||||
function isPromiseLike(obj) {
|
||||
return obj && isFunction(obj.then);
|
||||
}
|
||||
|
||||
|
||||
var trim = (function() {
|
||||
// native trim is way faster: http://jsperf.com/angular-trim-test
|
||||
// but IE doesn't have it... :-(
|
||||
|
||||
@@ -954,7 +954,7 @@ function $HttpProvider() {
|
||||
if (cache) {
|
||||
cachedResp = cache.get(url);
|
||||
if (isDefined(cachedResp)) {
|
||||
if (cachedResp.then) {
|
||||
if (isPromiseLike(cachedResp)) {
|
||||
// cached request has already been sent, but there is no response yet
|
||||
cachedResp.then(removePendingReq, removePendingReq);
|
||||
return cachedResp;
|
||||
|
||||
@@ -132,7 +132,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
|
||||
|
||||
if (timeout > 0) {
|
||||
var timeoutId = $browserDefer(timeoutRequest, timeout);
|
||||
} else if (timeout && timeout.then) {
|
||||
} else if (isPromiseLike(timeout)) {
|
||||
timeout.then(timeoutRequest);
|
||||
}
|
||||
|
||||
|
||||
@@ -303,7 +303,7 @@ function qFactory(nextTick, exceptionHandler) {
|
||||
} catch(e) {
|
||||
return makePromise(e, false);
|
||||
}
|
||||
if (callbackOutput && isFunction(callbackOutput.then)) {
|
||||
if (isPromiseLike(callbackOutput)) {
|
||||
return callbackOutput.then(function() {
|
||||
return makePromise(value, isResolved);
|
||||
}, function(error) {
|
||||
@@ -328,7 +328,7 @@ function qFactory(nextTick, exceptionHandler) {
|
||||
|
||||
|
||||
var ref = function(value) {
|
||||
if (value && isFunction(value.then)) return value;
|
||||
if (isPromiseLike(value)) return value;
|
||||
return {
|
||||
then: function(callback) {
|
||||
var result = defer();
|
||||
|
||||
Reference in New Issue
Block a user