q

Define empty Bluebird promise like in Q

本秂侑毒 提交于 2019-11-28 04:21:08
With Q I can define a new promise with: var queue = q(); But with Bluebird if I do: var queue = new Promise(); I get: TypeError: the promise constructor requires a resolver function How can I get the same result that I had with Q? This is a snippet of my code: var queue = q() promises = []; queue = queue.then(function () { return Main.gitControl.gitAdd(fileObj.filename, updateIndex); }); // Here more promises are added to queue in the same way used above... promises.push(queue); return Promise.all(promises).then(function () { // ... }); var resolver = Promise.defer(); setTimeout(function() {

AngularJS $q. Deferred queue

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:37:35
问题 I have array (f.e. it is queue of files): [{deferred: fileDef, data: file}, {...}, ...] Each fileDef and file send to upload function which return fileDef.promise and call fileDef.resolve or fileDef.reject after uploading. I want upload files in order: next file upload after previous file is loaded. Now I use var queue = []; var uploading = false; //file input callback call each time the user selects files function addAndUpload (file) { queue.push({deferred: $q.defer(), data: file}); if (

Unhandled rejection reasons (should be empty)

我只是一个虾纸丫 提交于 2019-11-28 02:43:29
问题 I'm getting into promises pattern with Q and I keep getting warning "[Q] Unhandled rejection reasons (should be empty)" in console. What em I doing wrong? http://jsfiddle.net/FpyDr/1/ function load(url) { var deferred = Q.defer(); $.ajax({ type: "GET", processData: false, dataType: "html", url: url, cache: false }).done(function (response, status, xhr) { deferred.reject(new Error("test error")); return; }).fail(function (xhr, status, error) { deferred.reject(new Error("ajax failed")); return;

Use jQuery or Q.Js for promises

社会主义新天地 提交于 2019-11-27 19:39:22
I'm looking into BreezeJs and there samples are using Q.js for promises to handle asynchronous calls. John Papa is also using Q. JQuery has promises as well . What are the differences between the two? Bergi Both are based on the Promises/A standard and implement a then method (though only current jQuery, they once had a incompatible pipe instead of then ). However, there are a few differences: Q has exception handling. All thrown errors in the async then callbacks will be caught and reject the promise (and will only get re-thrown if you call .end() ). Not sure whether I personally like that.

Q Promise Nodejs how to resolve in loop

自古美人都是妖i 提交于 2019-11-27 16:49:05
问题 i have code written in nodejs make me confusying using Q Promises theFunction() .then(function(data) { var deferred = Q.defer() var result = []; for(i=0; i < data.length; i++) { secondFunc(data.item) .then(function(data2) { data.more = data2.item }); result.push(data); } deferred.resolve(result); deferred.promise(); }); i want data in second function inside loop can push into result so my previous data is like this [ { id: 1, item: 1, hero: 2 }, { id: 1, item: 1, hero: 2 } ] and so like this

Node; Q Promise delay

僤鯓⒐⒋嵵緔 提交于 2019-11-27 16:25:18
Here are some simple questions based on behaviour I noticed in the following example running in node: Q('THING 1').then(console.log.bind(console)); console.log('THING 2'); The output for this is: > "THING 2" > "THING 1" Questions: 1) Why is Q implemented to wait before running the callback on a value that is immediately known? Why isn't Q smart enough to allow the first line to synchronously issue its output before the 2nd line runs? 2) What is the time lapse between "THING 2" and "THING 1" being output? Is it a single process tick? 3) Could there be performance concerns with values that are

Using Q.promises: how to catch an async throw?

為{幸葍}努か 提交于 2019-11-27 16:18:24
I'm using Q for promises, but when setting up some tests I discover I see way in catching async errors thrown inside a function that returns a promise. I tried to wrap it inside a Q.when and chained a fail and or as below a Q.fcall and a chained fail ,but I can't get it to work. var func = function(){ var deferred = Q.defer(); setTimeout(function(){ throw new Error("async error"); },100); return deferred.promise; } Q.fcall(func) .then(function(){ console.log("success"); }) .fail(function(err){ console.log(err); }) Is there a way to to this? The exception in the setTimeout is not related anyhow

AngularJS - fail resilence on $q.all()

老子叫甜甜 提交于 2019-11-27 15:51:45
问题 I'm trying to fill some local data resolving a series of remote calls. When every promise is resolved, I load the data and proceed. The method $q.all( [] ) does exactly this: $q.all([ this.getUserInfo(11) .then(function (r) { results.push(r) }), this.getUserConns() .then(function (r) { results.push(r) }), this.getUserCtxs() .then(function (r) { results.push(r) }) ]) .then(function () { console.log(results) }) Problem is, this code is not resilient. If any of these call fails, nobody gets the

Java - Split String by Number and Letters

喜你入骨 提交于 2019-11-27 14:52:34
问题 So I have, for example, a string such as this C3H20IO What I wanna do is split this string so I get the following: Array1 = {C,H,I,O} Array2 = {3,20,1,1} The 1 as the third element of the Array2 is indicative of the monoatomic nature of the I element. Same for O . That is actually the part I am struggling with. This is a chemical equation, so I need to separate the elements according to their names and the amount of atoms there are etc. 回答1: You could try this approach: String formula =

AngularJS Promises, $q, defer

梦想的初衷 提交于 2019-11-27 11:09:22
EDIT The first answer is the elegant one, but, as stated a few times in this question and another questions on stackoverflow, the problem is that the service and the controller run their thing before the data actually arrives. (Last comment on the first answer:) Yes, the problem is that the API calls finish AFTER the service runs and returns everything to the controller, see here screencast.com/t/uRKMZ1IgGpb7 ... That's my BASE question, how could I wait on all the parts for the data to arrive? It's like I'm saying it on repeat, how do we make a service that populates the array after the