q

Async waterfall equivalent with Q

岁酱吖の 提交于 2019-12-10 19:27:53
问题 I've got a single page which is an account settings page. In it, I allow my users to update their avatar (if they've attached an image), change their email (if it has been changed from the original), and change their name and password. Right now, I'm using async's waterfall method, but am swapping out async for Q since I prefer the syntax (and api). I'm wondering if this is the way that I should be using Q in replacement of async's waterfall. I'm doing something like this: exports

Break a dynamic sequence of promises with Q

强颜欢笑 提交于 2019-12-10 13:08:11
问题 I've got several promises (P1, P2, ... Pn) I would like to chain them in a sequence (so Q.all is not an option) and I'd like to break the chain at the first error. Every promise comes with its own parameters. And I'd like to intercept every promise error to dump the error. If P1, P2, .. PN are my promises, I don't have a clue how to automate the sequence. 回答1: If you have a chain of promises, they've all already started and there is nothing you can do to start or stop any of them (you can

Converting async code to q/promise code in nodejs

China☆狼群 提交于 2019-12-09 23:20:06
问题 I've read through a lot of different articles on promisejs but can't seem to get it to work for my code. I have async code that works and does what I need but it's very long and doesn't look as clean as it could with promise. Here's the two links I've been really looking into: http://jabberwocky.eu/2013/02/15/promises-in-javascript-with-q/ and https://spring.io/understanding/javascript-promises. mainCode.js accountModel.findOne({username: body.username}, function(err, usernameFound) { console

AngularJS: Avoid calling same REST service twice before response is received

此生再无相见时 提交于 2019-12-09 05:44:52
问题 I have two directives, each consuming the same factory wrapping a $q/$http call. angular.module("demo").directive("itemA", ["restService", function(restService) { return { restrict: "A", link: function(scope, element, attrs) { restService.get().then(function(response) { // whatever }, function(response) { // whatever }); } }; }]); angular.module("demo").directive("itemB", ["restService", function(restService) { return { restrict: "A", link: function(scope, element, attrs) { restService.get()

Serial execution with Q promises

拟墨画扇 提交于 2019-12-08 22:15:55
问题 I think I'm misunderstanding how Q promises work. I want my first promise to resolve before the next one starts, but that's not happening. Here is my code: var Q = require('q'); function doWork(taskName) { var deferred = Q.defer(); console.log('starting', taskName); setTimeout(function() { console.log('done with', taskName); deferred.resolve(); }); return deferred.promise; } doWork('task one') .then(doWork('task two')) .then(function() { console.log('all done'); }); This code produces: $ node

How to simplify Q promise example

让人想犯罪 __ 提交于 2019-12-08 05:22:35
问题 I was working on a simple application that makes sequential ajax calls, passing result of first call into the next one. Of course I don't want to go into the callback hell, and therefore look into Promises/A+ spec example and Q library. I have prepared an async function that should result in what I want. But I want an insight on how I can simplify the Sequential promise passing. For now I am still reading on how to best work with promises and deferred objects, so forgive me for the very naive

Angularjs custom directive using http promise not binding with template

六眼飞鱼酱① 提交于 2019-12-08 03:33:29
I am new to angularjs and want to add new feature in existing code. But code is not working as expected. I know i am not doing it the right way. Please correct me where is the mistake. I don't know why controller is not used but directive is used in this approach? Here is my custom service and custom directive directive code. Service code: angular.module("quickquiz-builder").service("SettingsService", function ($http, $q) { return { /* Return deffered promise response */ get: function() { var deferred = $q.defer(); $http.get('get.php') .then(function(response){ var config = response.data

Learning promises and exceptions vs. rejecting in Q

霸气de小男生 提交于 2019-12-07 22:22:19
问题 I'm under the impression that an exception inside a promise will trigger the subsequent fail-handler, but I'm not seeing it happen in this code: var Q = require('q'); function x() { console.log('x'); var deferred = Q.defer(); setTimeout( function() { console.log('resolving x'); deferred.resolve('hi'); }, 1000 ); return deferred.promise; } function y() { console.log('y'); var deferred = Q.defer(); setTimeout( function() { console.log('throwing in y'); throw new Error('Oih!'); }, 1000 ); return

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

会有一股神秘感。 提交于 2019-12-07 22:09:20
问题 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

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

自作多情 提交于 2019-12-07 16:21:47
问题 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