I\'m replacing $http
with Fetch API and got replaced $q
with Promise API. Because of that, Angular didn\'t run digest cycles anymore, thus UI didn\
Convert the ES6 promises created by the fetch API to AngularJS $q promises with $q.when.
$q.when
to convert ES6 promises to AngularJS promises1AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc...2 Since the promise comes from outside the AngularJS framework, the framework is unaware of changes to the model and does not update the DOM.
Use $q.when
to convert the external promise to an Angular framework promise:
var myRequest = new Request('flowers.jpg');
$q.when(fetch(myRequest)).then(function(response) {
//code here
})
Use $q Service promises that are properly integrated with the AngularJS framework and its digest cycle.
$q.when
Wraps an object that might be a value or a (3rd party) then-able promise into a
$q
promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.-- AngularJS $q Service API Reference - $q.when
Wrapping $q.when
will work but in my team's experience it will be very finicky and prone to error. As one example, returning $q.when
from inside the body of a Promise.then
function will still chain as a regular Promise
and you won't get a $digest on callbacks.
It also requires all authors to understand the difference between two very similar looking constructs (Promise/$q) and care about their concrete types for every level of an asynchronous call. If you are using modern conveniences like async
/await
(which abstracts the Promise types further), you're gonna be in even more trouble. Suddenly none of your code can be framework agnostic.
Our team decided it was worth committing a big monkey patch to ensure all the promises (and the async
/await
keywords) "just worked" without needing additional thinking.
Ugly? Yes. But we felt it was an okay tradeoff.
First we install the patch against Promise
in a angular.run
block:
angular.module(...).run(normalizePromiseSideEffects);
normalizePromiseSideEffects.$inject = ['$rootScope'];
function normalizePromiseSideEffects($rootScope) {
attachScopeApplicationToPromiseMethod('then');
attachScopeApplicationToPromiseMethod('catch');
attachScopeApplicationToPromiseMethod('finally');
function attachScopeApplicationToPromiseMethod(methodName) {
const NativePromiseAPI = window.Promise;
const nativeImplementation = NativePromiseAPI.prototype[methodName];
NativePromiseAPI.prototype[methodName] = function(...promiseArgs) {
const newPromiseArgs = promiseArgs.map(wrapFunctionInScopeApplication);
return nativeImplementation.bind(this)(...newPromiseArgs);
};
}
function wrapFunctionInScopeApplication(fn) {
if (!isFunction(fn) || fn.isScopeApplicationWrapped) {
return fn;
}
const wrappedFn = (...args) => {
const result = fn(...args);
// this API is used since it's $q was using in AngularJS src
$rootScope.$evalAsync();
return result;
};
wrappedFn.isScopeApplicationWrapped = true;
return wrappedFn;
}
}
If you want to support the use of async
/await
, you'll also need to configure Babel to always implement the syntax as Promises. We used babel-plugin-transform-async-to-promises.