q

Nested Promises

放肆的年华 提交于 2019-12-07 12:09:00
问题 I have a function that does a series of asynchronous actions that in turn execute loops of other asynchronous actions. I'd like to know when everything is complete. It seemed like a great time to get my head wrapped around promises. My code in the before-promise state boils down to something like this (hopefully in the simplification process I haven't rendered the example useless): myClass.prototype.doMaintenance = function() { var types = ['choreType1', 'choreType2']; types.forEach(function

$q.defer() not working with Angular service

醉酒当歌 提交于 2019-12-07 08:26:59
问题 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); });

Avoid multiple ajax requests angularJS

半城伤御伤魂 提交于 2019-12-07 04:14:12
问题 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

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

只愿长相守 提交于 2019-12-07 01:19:41
问题 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; }

AngularJS handling rejected resources in $q.all

微笑、不失礼 提交于 2019-12-06 20:41:59
问题 I'm trying to handle errors with my resources, and then handle rejection of resources in my $q.all() . This is my code: var user = User.get({id: 1}, function() { // Success }, function(response) { // Error return $q.reject(response); }); var promiseList = [user]; $q.all(promiseList).then(function(){ // Success <-- this seems to run all the time }, function(response) { // Error <-- this never seems to run but I want it to }); When my User resource receives a 404, the error callback handles it

NodeJS My SQL query with Chain promise

你说的曾经没有我的故事 提交于 2019-12-06 10:33:22
I have 3 function and i want to call this function to step by step, for example when i will call firs function and get result, i have to call second function and pass parameter returned from first call. after i will finish second call i have to call third function and pass parameter returned from second function. #1: getCategory = function (branch_id) { var deferred = q.defer(); var categoryData; var query = 'SELECT id,name,price,currency FROM category where branch_id=?'; pool.getConnection(function (err, connection) { connection.query(query, [branch_id], function (error, row, fields) { if

Jasmine clock tick & Firefox: failing to trigger a Q.delay method

爱⌒轻易说出口 提交于 2019-12-06 10:24:44
Lazy loading tests: I am trying to build a test for Jasmine to test a method that uses Q.delay . To go around the 10 seconds wait i'm using Jasmine's clock : jasmine.Clock.tick(10010); This works on Chrome but does not work on Firefox. I saw that the delay method of Q utilized setTimeout so I can't see any reason for the different behaviors. Any ideas why it fails on Firefox? With jasmine 2.0 and Q at the v1 tag , I'm able to run this spec: describe("testing", function() { beforeEach(function() { jasmine.clock().install(); }); afterEach(function() { jasmine.clock().uninstall(); }); it("should

Converting an array of promises from Selenium's findElements into an array of objects

Deadly 提交于 2019-12-06 05:15:14
I am using Selenium with node.js I am trying to do the following var driver = *webdriver instance*; var my_xpath = *an xpath string*; var ele; Q.all(driver.findElements(webdriver.By.xpath(my_xpath))).then(function(elements) { for (ele in elements) { console.log(ele.getText()); }; } I was under the impression that Q.all would convert the array of promises returned by driver.findElements into an array of values so that when I output ele.getText() it would be a value. However in this case the ele is still a promise. What am I missing here? Note that I realise that for the above example this is

Cordova android build error

╄→гoц情女王★ 提交于 2019-12-06 04:42:26
I get this error, when i try to build and run an cordova project on an android device: Running app on platform "android" via command "***/Documents/***/App/platforms/android/cordova/run" --device [Error: An error occurred while running the android project. /***/Documents/***App/platforms/android/cordova/node_modules/q/q.js:126 throw e; ^ Error executing "ant clean -f /***/Documents/***/App/platforms/android/build.xml": Build failed Any suggestion on fixing this problem? Check if "adb devices" command works. If it doesn't work ensure "android-tools-adb" is installed and then run the "adb

How do I do a callback chain with q?

邮差的信 提交于 2019-12-05 20:42:14
问题 I some problems understanding how to use "q" (https://github.com/kriskowal/q) a promises library for javascript: var delayOne = function() { setTimeout(function() { return 'hi'; }, 100); }; var delayTwo = function(preValue) { setTimeout(function() { return preValue + ' my name'; }, 200); }; var delayThree = function(preValue) { setTimeout(function() { return preValue + ' is bodo'; }, 300); }; var delayFour = function(preValue) { setTimeout(function() { console.log(preValue); }, 400); }; Q