angular-promise

How to return fully resolved promise?

Deadly 提交于 2019-12-02 11:12:32
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 something like this $q(expectedresponse) and get equivalent to above code. That is what I am after. Just like

How to Wait for Multiple Promises for All Data Success Callbacks

孤街浪徒 提交于 2019-12-02 10:38:16
I have this API call, but I don't receive the data in my successCallback in the same order as I send it. for (var i = 0; i < data.length; i++) { $http.post('/api/bla/blabla', $.param(data[i])) .then(successCallback, errorCallback); } var successCallback = function (response) { /* receive data in random order. assume its being send / handled so fast, thats its random which gets done first. */ }; Can I somehow wait for all data to be received, and then reorder it to the original ordering? or is there another solution. Use $q.all to get all the data in the right order. var promiseArray = []; for

How to use promise with two http requests

你说的曾经没有我的故事 提交于 2019-12-02 10:02:25
I'm trying to make couple of HTTP requests, one inside another, and I'm having a trouble restructuring my JSON object. I have a factory function, first of all i'm trying to get all the teams, each team has an Id, and then i'm getting all the news for each team with the id related, and putting that news in the first JSON object. but this is not working ! .factory('teamsFactory', function ($http,CacheFactory,LocaleManager,$q) { teams.Getteams= function() { var zis=this; var promise=$http({method:"GET",url:"http://www.myserver/teams"}); promise.then(function(response){ for (var i=0;i<response

Return a Promise that promises to throw error

给你一囗甜甜゛ 提交于 2019-12-02 09:45:52
I am fairly new to using promises, I mostly stick to straight up callbacks. The following code happens to be found in an Angular Service, but that doesn't really matter that much, it's mostly regards how to create a promise that will promise to throw a certain error. So this is how it would happen with callbacks being incorporated, which avoids the problem: var client = algolia.Client('ApplicationID', 'apiKey'); var indices = { users: client.initIndex('users'), topics: client.initIndex('topics') } return { search: function(indexName, searchTerms, cb){ var index = indices[indexName]; if(index){

ui-router: async data in templateUrl function

ぐ巨炮叔叔 提交于 2019-12-02 09:11:54
I've run into quite a novel problem with ui router. I'm basically trying to query my API for a template url, which is stored and returned as a Resource object. The problem, is it seems that the templateUrl function is returning as undefined. My code is as follows: (function() { 'use strict'; angular.module('myApp') .config(function ($stateProvider, PageProvider, $httpProvider) { $stateProvider .state('core', { abstract: true, templateUrl: 'app/core/core.index.html', controller: 'CoreController', controllerAs: 'vm' }) /* Parent page view */ .state('core.page', { url: '/:slug', views: { 'page

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

◇◆丶佛笑我妖孽 提交于 2019-12-02 08:25:33
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( (resp) => { this.states = resp.data; })); promise.push(this.getTerritories(Id).then( (resp) => { this

Firebase Array Fetches Not Loading on Page Load

ぐ巨炮叔叔 提交于 2019-12-02 08:19:23
// 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) { $rootScope.userID = user.uid; // Run query to pull user records. $timeout(function() { var userID =

AngularJS Sequentially Chain Promises in forEach Loops

…衆ロ難τιáo~ 提交于 2019-12-02 08:08:05
I have been doing lots of searching trying to find a solution, and believe it ultimately comes down the the promise as my data is returned but all at the end, where as I need it through each iteration. I have the vm.planWeek.dinner that I loop through each row, and append 'menuType' and the 'trackMenuIds' array to it, which I then use in my MongoDB call for search criteria. This all works fine, but the key element, is with each factory call returned, I add the returned item's id to the 'trackMenuIds' array. The reason for this is, it builds an array of items I already have returned, so they

Response of Http Api call promise is undefined - Angular 2

喜夏-厌秋 提交于 2019-12-02 07:19:16
I have created an api in WebAPI as below. public HttpResponseMessage Get() { var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(JsonConvert.SerializeObject("Hello World"), Encoding.UTF8, "application/json"); return response; } I am trying to call it from Angular as below Service.ts @Injectable() export class DemoService { constructor(private http:Http){} GetHttpData(){ return this.http.get('http://localhost:54037/api/home') .map((res:Response)=>res.json()); } Component: export class AppComponent implements OnInit { data2: String; constructor(private

angularjs simple .then or $q service in async requests

流过昼夜 提交于 2019-12-02 04:28:54
I don't know what is the exactly difference between AngularJS $q service and simply using .then() after async request. Simple example with .then() : function InboxService($http) { this.getEmails = function getEmails() { return $http.get('/emails'); }; } And when using the service (just part of code): InboxService.getEmails() .then(function (response) { // use response }); What is the difference with $q service with resolve and reject ? What is the difference with $q service with resolve and reject ? I assume you are asking about the usage of var deferred = $q.defer() with subsequent deferred