angular-promise

Javascript: How to iterate on array using promises?

删除回忆录丶 提交于 2019-12-04 00:18:51
问题 LIVE DEMO Given the following function: function isGood(number) { var defer = $q.defer(); $timeout(function() { if (<some condition on number>) { defer.resolve(); } else { defer.reject(); } }, 100); return defer.promise; } and an array of numbers (e.g. [3, 9, 17, 26, 89] ), I would like to find the first "good" number. I would like to be able to do this: var arr = [3, 9, 17, 26, 89]; findGoodNumber(arr).then(function(goodNumber) { console.log('Good number found: ' + goodNumber); }, function()

angularjs handling $resource $promise errors

瘦欲@ 提交于 2019-12-03 20:18:49
Could somebody help me figure out how to return hard-coded data in my AngularJS factory if there is an error connecting to my API. My hard-coded data are located in another factory called "dataFactory". Appreciate the assistance. service.factory("ScheduleFactory", ['$http', '$resource', '$q', 'dataFactory', function($http, $resource, $q, dataFactory) { var objFactory = {}; objFactory.getDaysOfWeek = function(includeWeekends) { var days = []; var API = $resource(restURL + '/daysOfWeek/'); API.query() .$promise .then(function(data) { isEmpty = (data.length === 0); if (!isEmpty) { days = data; };

Injecting a resolved promise into service

走远了吗. 提交于 2019-12-03 16:44:55
问题 I need to get some information (a schema) from the server before I set up a bunch of services that depend on that information. My server provides a schema that defines various properties of a model. In my angular code, I have a service that gets this schema: services.factory('schema', function($q, $http) { var deferred = $q.defer(); $http.get('schema/').then(function(response) { schema = // some function of response.data deferred.resolve(schema); }, function() { deferred.reject('There was a

Typescript async/await and angular $q service

非 Y 不嫁゛ 提交于 2019-12-03 01:42:27
New TypeScript async/await feature uses ES6 promises. AngularJS uses $q service promises with slightly different interface. Is there any way to use TypeScript async/await feature with $q service promises? You can make it work like this: angular.module('your app') .run(['$window', '$q', function($window, $q) { $window.Promise = $q; }]); I do not think you will be able to use them directly. But it should be quite easy to convert q promise into a++ promise, something like this: function Convert<T>(qPromise): Promise<T> { return new Promise<T>((resolve, reject) => { qPromise.then((result: T) =>

Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?

丶灬走出姿态 提交于 2019-12-03 01:08:09
I am trying to improve my knowledge of Angular2 by migrating an application currently written in Angular1 One feature in particular has me stumped. I am trying to replicate a feature where a calling function waits to proceed until the function it is calling has completed a loop of promises. In angular one the function I am calling looks basically like this: this.processStuff = function( inputarray, parentnodeid ) { var promises = []; var _self = this; angular.forEach( inputarray , function( nodeid ) { switch ( parentnodeid ) { case ‘AAA’ : var component = _self.doMoreStuff( nodeid,

multiple promises running in parallel, $q.all needs chaining?

我与影子孤独终老i 提交于 2019-12-02 22:15:03
问题 I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally call .then( of $q.all Here is the code getAllLocations() { //make a promise call for all here . var promise = []; ̶p̶r̶o̶m̶i̶s̶e̶.̶p̶u̶s̶h̶(̶t̶h̶i̶s̶.̶g̶e̶t̶A̶l̶l̶L̶o̶c̶a̶t̶i̶o̶n̶s̶(̶I̶d̶)̶.̶t̶h̶e̶n̶(̶ promise.push(this.getLocations(Id).then( (locationsData) => { this.locations = locationsData; })); promise.push(this.getAllStates(Id).then(

How to return fully resolved promise?

杀马特。学长 韩版系。学妹 提交于 2019-12-02 22:01:08
问题 I am trying to play around with $q, write some tests, try to stub promises etc. and I wondered if there is a way to return fully resolved promise like one can do it with whenjs, when("stuff to return), something that would be equal to this function fullyResolvedPromise(expectedResponse) { var dfd = $q.defer(); dfd.resolve(expectedResponse); $rootScope.$apply(); return dfd.promise; } Clarification: I know this code works, but I want to do it without writing this function. I want to do

Firebase Array Fetches Not Loading on Page Load

巧了我就是萌 提交于 2019-12-02 19:29:15
问题 // Movements Controller app.controller("MovementsCtrl", ["$scope", "$rootScope", "$filter", "$timeout", "$route", function($scope, $rootScope, $filter, $timeout, $route) { $rootScope.pageName = "Movements"; var date = new Date(); var currentDate = $filter('date')(new Date(), 'MM/dd/yyyy'); $scope.labels = []; $scope.data = []; $scope.recordsArray = []; // Converts records object into an array. console.log($scope.recordsArray); firebase.auth().onAuthStateChanged(function(user) { if (user) {

AngularJS: $q.race() in old angular versions

ぃ、小莉子 提交于 2019-12-02 15:21:40
问题 Angular 1.5.8 implements a $q.race() method which takes an array of promises and returns a promise which is resolves with the value of the first resolved promise. However i am stuck with angular 1.4 for now and need some kind of functionality like a $q.any or $q.race method. Currently I use flags inside .then() to "remember" the state of promises which obviously is not ideal. var resolvedPromise = null; promise1.then(function(data){ if(!resolvedPromise === 'promise2'){ resolvedPromise =

Chaining Angular promise rejections

笑着哭i 提交于 2019-12-02 11:55:31
I have a chained promise and in the case of a rejection for either of the promises, I need to perform an async operation (get the translated error message). As I've already got a chained promise on success, I assume it's not possible to also chain on rejection - I am attempting to simply nest the async calls, but I'm not getting the resolved promise back from deferred.reject(deferredRejection.promise); below. Pointers appreciated! login: function(email, password) { var deferred = $q.defer(); AuthService.login(email, password).then(function(response) { var user = { 'accountToken': response