Not sure if I'm understanding properly your question, but as I see in your code, it'll only work fine when pCode[i + 1] !== "-" .
Dunno what you need to do in the '...' part, but you should do the resolve inside the if part and inside the 'then' part of the OtherService call. Another possibility might be to reject the pCode === '-' inside SomeService and in the catch block try to recover with OtherService call. Probably whatever you're doing after that might be handled in a finally block? Hope it helped... 2016-03-29 17:00 GMT+01:00 Martin Wawrusch <[email protected]>: > I don't know if there is a promise way, but you could use the async > library to handle your async processing. The code you have here is pretty > much untestable, and I spotted already one bug by just looking at it (the > statement after continue is never called) > > > On Tuesday, March 29, 2016 at 7:44:06 AM UTC-7, Zeit Rapha wrote: >> >> hi, >> >> >> my problem is a more generell problem. i am asking here to find out, if >> there is a special angularjs feature, which reduces the complexity of a for >> loop with promises. >> >> >> a short example: >> i got a function surrounded by a $q. in this function, a code is iterated >> through a for-loop. if the value of the code is "0", continue with the next >> step in the for-loop. otherwise, go through some other asynchronous service >> functions to set the variable myData correctly. >> >> >> return $q(function (resolve, reject) { >> var pCode = "1050"; >> var myData = {}; >> for (var i = 0; i <= pCode.length; i++) { >> var lcHumanIdx = i + 1; >> if (pCode[i] === "0") { >> continue; i = i + 1; >> } else { >> if (lcHumanIdx === 1) { >> SomeService.asyncFunction(pCode[i]) >> .then(function (pElement) { >> if (pCode[i + 1] !== "-") { >> myData.translation = pElement; >> } else { >> OtherService.asyncFunction() >> .then(function (pOtherElement) { >> myData.translation = pOtherElement; >> }) >> } >> ... >> resolve(myData); >> // or handling with many promises >> // $q.all(collectedPromises).then(function() { >> console.log("Done"); }) >> }); >> >> >> before i go and code, i just wanted to ask, whats the best practise to >> realize such a problem. are there any specific angularjs solutions (like >> bluebirds promiseWhile/-For)? the problem in this example is, that the >> variable myData is {} every time! the variable is resolved before the >> asyncronous functions completed. my aim is to set all "attributes" of >> myData (f.e. myData.translation) correct, and then resolve it. what would >> be the best realization? >> > -- > You received this message because you are subscribed to the Google Groups > "AngularJS" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
