How to use the axios library with AngularJS

How to use the axios library with AngularJS Bring its ES6 promises into the AngularJS context using $q.when: // axios example ̶a̶x̶i̶o̶s̶.̶g̶e̶t̶(̶u̶r̶l̶)̶.̶t̶h̶e̶n̶(̶(̶r̶e̶s̶p̶o̶n̶s̶e̶)̶ ̶=̶>̶ ̶{̶ $q.when(axios.get(url)).then((response) => { $scope.axiosResult = response.data; }); Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Also use the $timeout … Read more

Make angular.forEach wait for promise after going to next object

Before ES2017 and async/await (see below for an option in ES2017), you can’t use .forEach() if you want to wait for a promise because promises are not blocking. Javascript and promises just don’t work that way. You can chain multiple promises and make the promise infrastructure sequence them. You can iterate manually and advance the … Read more

How to create a AngularJS promise from a callback-based API

How to create a AngularJS promise from a callback-based API To create an AngularJS promise from a callback-based API such as WifiWizard.connectNetwork, use $q.defer: function connectWifi(wifi_ssid) { var future = $q.defer(); var win_wifi = future.resolve; var fail_wifi = future.reject; WifiWizard.connectNetwork(wifi_ssid, win_wifi, fail_wifi); return future.promise; }; The above example returns a $q Service promise that either … Read more

How do I sequentially chain promises with angularjs $q?

Redgeoff, your own answer is the way I used to translate an array into a chained series of promises. The emergent de facto pattern is as follows : function doAsyncSeries(arr) { return arr.reduce(function (promise, item) { return promise.then(function(result) { return doSomethingAsync(result, item); }); }, $q.when(initialValue)); } //then var items = [‘x’, ‘y’, ‘z’]; doAsyncSeries(items).then(…); Notes: … Read more

ES6 Promise not Updating AngularJS DOM [duplicate]

Use $q.when to convert ES6 promises to AngularJS promises AngularJS 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…1 Since the promise … Read more

How can I access a variable outside a promise `.then` method?

getUser() is not returning anything. You need to return the promise from the Spotify.getCurrentUser(), and then when you return names within that it is returned by the outer function. function getUser() { if ( $localStorage.token == undefined) { throw alert(“Not logged in”); } else { return Spotify.getCurrentUser().then(function(data) { var names = JSON.stringify(data.data.display_name); console.log(names) return names; … Read more

AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

Promises have three states Pending – this is how promises start. Fulfilled – this is what happens when you resolve a deferred, or when the return value from .then fulfills, and it generally analogous to a standard return value. Rejected – This is what happens when you reject a deferred, when you throw from a … Read more