I ended up writing a piece of functional code that has no natural exit point for me to fetch the final output from. It's the sort of code that performs a HTTP request which results in N other HTTP requests and together they populate a data structure. It seems I'll need to use JS promises... but what am I promising exactly? That an unknown number of HTTP calls and nested loops will complete?
@thor
If you're not worried about presenting all of the information at once, using a promise allows the browser to go off and do something in the background, activating a different function when it completes the task. So the promise could take care of your insert when the promised get request completes
@skypage It's a node.js program. It's going to generate a report. I'm performing requests to a 3rd party API. Could theoretically run in the browser, but I'm not sure if I want to deal with cross-domain AJAX issues that may ensue.
@skypage If I did do it in the browser, it would simplify some things, granted that I don't care about the order my data is presented in, because I could just perform a bunch of inserts in that case. If I were using Angular, though, I suppose I'd be in a bit of trouble, since controllers don't really operate on the principle of callbacks. (I suppose this is where people will begin to recommend React to me.)
@thor
And my mistake on assuming browser JS. I like my compiled server languages.
@skypage In this particular case, simple synchronous calls would save me so much trouble. I'm not in a hurry to generate the output, and simple console text would be sufficient for the task. I'll only be running this thing once a month.
@skypage Bringing out an entire JVM for this seems a bit extreme. And I bet the HTTP requests would be asynchronous in both Scala and Java anyway, so it wouldn't make things easier for me.
@thor
There is a compiler plugin for Scala that outputs JS instead of JVM byte code 😉
@thor
I would expect you could go both synchronous or async in Java. Everything I've done in Scala is async by default
@thor
Much easier to do what you want with your output when it gets back to you in Scala for async stuff, though.
@thor
If you want an example of dealing with async data in Scala, you can see starting at line 123 how I apply some manipulations on my data when the request gets around to returning.
@thor
In Angular, you could do it with promises as well. It would just be recommended you do that in a service instead of a controller.