q

Nodejs / Q : Chaining promises sequentially

旧城冷巷雨未停 提交于 2019-11-27 02:29:28
问题 I want to do something really simple, but i don't understand a little thing ... var Q = require('q'); var funcs = ["first", "second", "third", "fourth"]; function main(){ // really don't know how to chain sequentially here ... var result = Q(); funcs.forEach(function (f) { result = treat(f).then(f); }); } function treat(t){ var deferred = Q.defer(); setTimeout(function(){ deferred.resolve("treated "+ t); },2000); return deferred.promise; } main(); I would like each element of my funcs array

Is there a way to return early in deferred promise?

你说的曾经没有我的故事 提交于 2019-11-27 02:13:00
So I have a promise which contains multiple checks like this function test(){ var deferred = q.defer() var passed = false if(!passed){ deferred.reject("Don't proceed") //return } else { if(!passed){ deferred.reject("Don't proceed") } else { if(!passed){ deferred.reject("Don't proceed") } else { setTimeout(function(){ console.log("Hello"); deferred.resolve() }, 100); } } } return deferred.promise } This looks bad because there is a pyramid at the bottom. I'm wondering is there something like return in 'q' which can make the code look better? For example like this: function test(){ var deferred

Node.js Asynchronous Library Comparison - Q vs Async

若如初见. 提交于 2019-11-27 01:04:17
问题 I have used kriskowal's Q library for a project (web scraper / human-activity simulator) and have become acquainted with promises, returning them and resolving/rejecting them, and the library's basic asynchronous control flow methods and error-throwing/catching mechanisms have proven essential. I have encountered some issues though. My promise.then calls and my callbacks have the uncanny tendency to form pyramids. Sometimes it's for scoping reasons, other times it's to guarantee a certain

Define empty Bluebird promise like in Q

痞子三分冷 提交于 2019-11-27 00:21:25
问题 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

Use jQuery or Q.Js for promises

◇◆丶佛笑我妖孽 提交于 2019-11-26 22:49:52
问题 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? 回答1: 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

AngularJS Promises, $q, defer

做~自己de王妃 提交于 2019-11-26 22:20: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

Promises: Repeat operation until it succeeds?

时光毁灭记忆、已成空白 提交于 2019-11-26 22:17:41
问题 I want to perform an operation repeatedly, with an increasing timeout between each operation, until it succeeds or a certain amount of time elapses. How do I structure this with promises in Q? 回答1: All the answers here are really complicated in my opinion. Kos has the right idea but you can shorten the code by writing more idiomatic promise code: function retry(operation, delay) { return operation().catch(function(reason) { return Q.delay(delay).then(retry.bind(null, operation, delay * 2)); }

Proper way to skip a then function in Q Promises

柔情痞子 提交于 2019-11-26 20:35:49
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 the conditions fail, I have to skip all the series of operations and I have to execute the done

How do I tell if an object is a Promise?

筅森魡賤 提交于 2019-11-26 19:21:59
Whether it's an ES6 Promise or a bluebird Promise, Q Promise, etc. How do I test to see if a given object is a Promise? How a promise library decides If it has a .then function - that's the only standard promise libraries use. The Promises/A+ specification has a notion called then able which is basically "an object with a then method". Promises will and should assimilate anything with a then method. All of the promise implementation you've mentioned do this. If we look at the specification : 2.3.3.3 if then is a function, call it with x as this, first argument resolvePromise, and second

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

雨燕双飞 提交于 2019-11-26 18:35:40
问题 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)