q

Q Promise Nodejs how to resolve in loop

余生颓废 提交于 2019-11-29 02:21:33
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 [ { id: 1, item: 1, hero: 2, more: { list: 1 } }, { id: 1, item: 1, hero: 2, more: { list: 4 } } ] I've

Java - Split String by Number and Letters

馋奶兔 提交于 2019-11-29 01:48:12
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. You could try this approach: String formula = "C3H20IO"; //insert "1" in atom-atom boundry formula = formula.replaceAll("(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z]

AngularJS - fail resilence on $q.all()

可紊 提交于 2019-11-29 01:24:12
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 fish! Wrapping the calls in a try/catch statement, simply causes $q.all() to entirely ignore the entry,

How do you properly return multiple values from a promise?

拈花ヽ惹草 提交于 2019-11-28 17:08:58
I've recently run into a certain situation a couple of times, which I didn't know how to solve properly. Assume the following code: somethingAsync() .then( afterSomething ) .then( afterSomethingElse ) function afterSomething( amazingData ) { return processAsync( amazingData ); } function afterSomethingElse( processedData ) { } Now a situation might arise where I would want to have access to amazingData in afterSomethingElse . One obvious solution would be to return an array or a hash from afterSomething , because, well, you can only return one value from a function. But I'm wondering if there

How to maintain a promise-like API in this case?

拟墨画扇 提交于 2019-11-28 14:37:13
function foo(options) { if(!isValid(options)) { // I want to return a resolved promise here to permit client code to continue without a failure } return promisifiedThirdPartyApi(options); // Does not handle an invalid options object successfully } How can I idiomatically return a resolved promise in the "invalid" case? Native Promises Take a look at the native Promise object's static methods resolve and reject . function foo(options) { if(!isValid(options)) { return Promise.resolve(); } return promisifiedThirdPartyApi(options); } Angular $q Use $q.when to return a resolved Promise from some

asynchronous or promised condition for array filter

最后都变了- 提交于 2019-11-28 11:45:58
I need to filter an array based on a condition that can only be checked asynchronously. return someList.filter(function(element){ //this part unfortunately has to be asynchronous }) Is there any nicer way to do it with promises than what I already have? This snippet uses Q for promises, but you can actually assume any proper promises implementation. return q.all(someList.map(function (listElement) { return promiseMeACondition(listElement.entryId).then(function (condition) { if (condition) { return q.fcall(function () { return listElement; }); } else { return q.fcall(function(){}); } }); }));

How can I limit Q promise concurrency?

一个人想着一个人 提交于 2019-11-28 09:12:48
How do I write a method that limits Q promise concurrency? For instance, I have a method spawnProcess . It returns a Q promise. I want no more than 5 process spawned at a time, but transparently to the calling code. What I need to implement is a function with signature function limitConcurrency(promiseFactory, limit) that I can call like spawnProcess = limitConcurrency(spawnProcess, 5); // use spawnProcess as usual I already started working on my version, but I wonder if anyone has a concise implementation that I can check against. I have a library that does this for you https://github.com

Recursive Promises?

允我心安 提交于 2019-11-28 09:08:31
I would like to iterate over all files located in the HTML 5 file system and have some event get started once the iteration is complete. Since this is async + promises im having a hard time trying to grasp how it should work. I am using a angularJS and have created a service to encapsulate html 5 file system specific features. This is the recursive function: function walkDirectory(path) { fileSystem.getFolderContents(path) //this is the services and it returns a promise containing all files in the current folder or directory .then(function(entries) { for (var i = 0; i < entries.length; i++) {

Nodejs / Q : Chaining promises sequentially

落花浮王杯 提交于 2019-11-28 08:50:47
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 to be "treated" sequentially, the output would then be something like : treated first //2 seconds later

How to call Q promise notify within the promise chain

≯℡__Kan透↙ 提交于 2019-11-28 08:20:13
问题 I need helps on notify() within the promise chain. I have 3 promise base functions connect() , send(cmd) , disconnect() . Now I would like to write another function to wrap those call in following manner with progress notification. function bombard() { return connect() .then(function () { var cmds = [/*many commands in string*/]; var promises = _.map(cmds, function (cmd) { var deferred = Q.defer(); deferred.notify(cmd); send(cmd).then(function (result) { deferred.resovle(result); }); return