q

Passing parameters to promise's success callback in angularjs $q

霸气de小男生 提交于 2019-12-05 19:55:33
I realize this is a very similar question to this one . But I'm still unclear on how to do it in my situation. Just need some help with a successful callback. This is what works: function getStuff(accountNumber) { var logMessage = 'POST GetStuff'; return $http.post(GetStuff, { custId: accountNumber }) .then(log); } function log(response) { logger.debug(response); return response; } This is what I want to accomplish: function getStuff(accountNumber) { var logMessage = 'POST GetStuff'; return $http.post(GetStuff, { custId: accountNumber }) .then(log(response, logMessage); } function log(response

$q.defer() not working with Angular service

↘锁芯ラ 提交于 2019-12-05 14:01:18
I'm new to angular's $q and I'm trying to build a service that makes an API call and returns it back to the controller. Problem: No matter how I seem to format it, the service returns right before it gets to $http.get(...) Service: // methods: query new, get existing makeRequest: function(url, following) { // create promise var deferred = $q.defer(); $http.get(url, { params: { "following": JSON.stringify(following) } }) .then(function(res) { console.log(res); deferred.resolve(res.data); }); return deferred.promise; }, getFeed: function(user) { console.log('>> userService[getUser]: retrieving

Avoid multiple ajax requests angularJS

我是研究僧i 提交于 2019-12-05 10:52:35
I am trying to avoid multiple ajax requests to the server in a factory. I already added a small caching service, but it is not enough for what I aim: this factory can be called several times before the server responds, causing the generation of multiple requests to the server. To avoid this I added a second promise object, which if the AJAX request have been performed and the object is not yet in cache, than it should wait for a second promise to be resolved, but looks like I am missing something. This is my code: myApp.factory('User', ['Restangular', '$q', function (Restangular, $q) { var

TypeError: Cannot read property 'then' of undefined angularjs-grunt test

别说谁变了你拦得住时间么 提交于 2019-12-05 06:55:42
I'm using $q service to make an async calls. I can't resolve 'then' and 'defer' in unit tests using karma. The below is my controller code. scope.filterUrls = [{url:'page1'}, {url: 'page2'}, {url:'page-error'}]; scope.bindFilters = function () { angular.forEach(scope.filterUrls, function (data) { scope.getFilterData(data.url, '').then(function (result) { if (data.url === 'page1') { scope.moduleData.index = result.data; } else if (data.url === 'page2') { scope.moduleData.page2 = result.data; } }); }); } scope.getFilterData = function (filterUrls, params) { // $q service object var deferred = q

What $q.defer() really does?

走远了吗. 提交于 2019-12-04 23:51:23
问题 I'm learning about Angular JS and on the moment I'm trying to understand about promises and async programming and I have this doubt about $q.defer() . My point is the following: usually when people work with promises they do something like that, considering that $q is already available function someAsyncFunction() { var deferred = $q.defer(); /* Do things and if everything goes fine return deferred.resolve(result) otherwise returns deferred.reject() */ return deferred.promise; } What is this

Chaining an arbitrary number of promises in Q

若如初见. 提交于 2019-12-04 22:16:40
问题 I want to send an HTTP request N times. I want to eventually have information about the results of each of those requests. Running the request function once works great. Here's the HTTP request function using Q.defer(): function runRequest() { var deferred = Q.defer(), start = (new Date).getTime(), req = HTTP.request(options, function(res) { var end = (new Date).getTime(), requestDetails = { reqStatus: res.statusCode, reqStart: start, reqEnd: end, duration: end - start } deferred.resolve

Promise how to implement in this situation?

半腔热情 提交于 2019-12-04 20:19:26
i have a problem with sync using fs.readfile, they are not inicializing at the first time, if i try request again, then i have results. I know i can use promise and i find something useful with Q from Kriskowal . I tried it but without success. I didn´t understand very well how to implement it. If someone can help with this, i will be eternally grateful. code: "use strict" var Q = require('q'); var fs = require('fs'); var arrayZipBand = []; var jsonZipCode = []; var arrayParsedZipcodeBr = []; exports.parse = function(opts) { if (opts.zipBand.constructor != Array) { opts.zipBand = [opts.zipBand

BreezeJs with dedicated web worker

大兔子大兔子 提交于 2019-12-04 19:09:00
I am trying to initialize a Breeze manager inside a 'Web Worker'. RequireJs, knockout, q, breeze are being imported inside the worker. After a call to: EntityQuery.from('name').using(manager).execute() , the following error appears: Uncaught Error: Q is undefined. Are you missing Q.js? See https://github.com/kriskowal/q . A live preview is uploaded here http://plnkr.co/edit/meXjKa?p=preview (plunk supports downloading for easier debug). EDIT -- relevant code Worker.js importScripts('knockout.js', 'q.js', 'breeze.js', 'require.js'); define('jquery', function () { return jQuery; }); define(

$q 'finally' not working in IE8

£可爱£侵袭症+ 提交于 2019-12-04 17:26:08
问题 Angular 1.2 replaced always with finally on promises. So what was once this: $http.get('/myurl').always(handler); Now needs to be this: $http.get('/myurl').finally(handler); But I am getting the error "expected identifier" in IE8. How can I make this work in IE8? 回答1: Found it: https://github.com/angular/angular.js/commit/f078762d48d0d5d9796dcdf2cb0241198677582c $http.get('/myurl')["finally"](handler); 来源: https://stackoverflow.com/questions/18254205/q-finally-not-working-in-ie8

q.js: difference between resolve() and fulfill()

大兔子大兔子 提交于 2019-12-04 16:29:45
问题 I'm still unclear on the difference between calling a resolver's resolve() vs fulfill()? I see both the functions and the terms "resolve a promise" and "fulfill a promise" batted around a lot. When should I be using each? 回答1: You should use resolve . deferredPromise.resolve(nextPromise) means that anything waiting for deferredPromise will now wait for nextPromise . If nextPromise is not a promise at all, it gets turned into a fulfilled promise which goes on to inform anything waiting for it