q

Proper way to skip a then function in Q Promises

可紊 提交于 2019-12-27 14:42:12
问题 In my code, based on a specific condition, I would like to skip to the done function, irrespective of all the then functions. The original version of this question is in the edits. The following is the actual problem I am dealing with. Sorry for the inconvenience Actual Problem : I am reading a file and processing it. If the contents of the file match certain conditions, I have to do a series of operations on the file system (say read and write few files) and then to execute done function. If

While loop with promises

一世执手 提交于 2019-12-27 10:31:07
问题 What would be the idiomatic way to do something like a while loop with promises. So: do something if the condition still stands do it again repeat then do something else. dosomething.then(possilblydomoresomethings).then(finish) I've done it this way I was wondering if there were any better/more idomatic ways? var q = require('q'); var index = 1; var useless = function(){ var currentIndex = index; console.log(currentIndex) var deferred = q.defer(); setTimeout(function(){ if(currentIndex > 10)

How can I chain together groups of promises?

一笑奈何 提交于 2019-12-25 18:16:07
问题 I am using the Q javascript promises library and am running in a browser, and I want to figure out how to chain together groups of promises so that each group gets executed sequentially. For example, if I have items A, B, C, and D, I want to group A and B together and then C and D together, so that both A and B must fulfill before C and D get executed. I created this simple jsfiddle to show my current attempt. var work_items = [ 'A','B','C','D','E','F','G','H','I' ]; var n = 2; // group size

Chaining promises without using 'then' with Q library

梦想与她 提交于 2019-12-25 07:59:14
问题 I'm trying to chain Q promises without 'then', so eventually the chain would look like this: var foo = new Foo(); foo .method1() .method2() .method3(); How to implement foo's methods so each one is executed once the promise of the previous one is resolved? This question was marked as exact duplicate of this one but I'm trying to implement this using Q lib, not jQuery. 回答1: I'm not sure if you are going to gain anything with this. I suppose that you can do something like this: function Foo() {

$q.all - take only those that resolved

感情迁移 提交于 2019-12-25 03:24:31
问题 in case like that: getCol = (colId)-> dfrd = $q.defer() if colId == "bacon" dfrd.reject() else dfrd.resolve colId dfrd.promise getCols = (columns)-> $q.all(_.map(columns, (cs)-> getCol(cs))) getCols(['eggs','juice']).then (cols)-> console.log cols # works getCols(['eggs','juice','bacon']).then (cols)-> console.log cols # not even getting here So, in getCols() how can I return only those promises that's been resolved? 回答1: $q.all is meant only to resolve when all of the promises you pass it

Why am I getting a TypeError when querying through breeze during a compose in durandal?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 11:42:42
问题 I'm getting the following error from breeze and q.js when my view and model are being bound by a compose binding statement using durandal. I wrote the view in question in isolation and it works great until I try to do a compose binding with it, it throws this error. I tried moving all the custom properties from my entity constructor function to the initializer and also defering evalution of my computed's but that did nothing to thwart the error. I'm not sure which framework is causing this

Wrapping jQuery $.ajax behind a facade in q.js without refactoring

人走茶凉 提交于 2019-12-24 08:14:48
问题 It's possible this should be on code review, but here we go! I have a fairly large application with a lot of ajax calls. I started using Q for some async stuff, and figured I would wrap the ajax calls in Q to ensure all async methods have the same signature. I'm using a global facade method, so my ajax calls look like: App.ajax( config ).then( doWhatever ); with App.ajax looking something like this: ajax: function( config ){ var ajaxReturn = $.ajax( config ); ajaxReturn.error(function( xhr ){

“Cannot read property 'defer' of undefined”. error in AngularJs. Does anyone have a solution?

微笑、不失礼 提交于 2019-12-24 07:28:21
问题 In my service "customerService" I am injecting the $q object which exposes the deferred object. In my function "getCustomers" I am trying to retrieve the deferred object but I receive an error: "Cannot read property 'defer' of undefined" when I run the $q.defer() method. I have already looked at similar solutions on stackoverflow but none were adequate. What could be the cause of this error? var mysql = require("mysql"); //creates sql database connection var connection = mysql

Q promise. Difference between .when and .then

拈花ヽ惹草 提交于 2019-12-24 03:18:06
问题 I though this code will work: var promise = function(val) { var _val = val; return setTimeout(function(_val) { var newVal = val / 10; return { newVal : newVal, message : 'it just to be a ' + val }; }, 3000); }; Q.when(promise(400)).then(function(obj) { return console.log('jaaaaj !', obj); }, function() { return console.log('no yet...'); }); JSFiddle My thinking was like: when setTimeout finish its job after four seconds, Q library will catch return in first callback and show object with two

AngularJS Promises $q.all and SignalR

匆匆过客 提交于 2019-12-24 02:28:04
问题 I have checked number of answers regarding promises, but I can't get my code working (perhaps I'm doing something terribly wrong in other place) In general I'm working on small test application using AngularJS and SignalR. I have signalR service which looks like this: (function () { 'use strict'; var serviceId = 'signalRSvc'; angular.module('app').service(serviceId, ['$rootScope', signalrcontext]); function signalrcontext( $rootScope) { var performanceHub = null; var connection = null; var