Building my first “serious” Node.js project (using Express).
I need to use several calls to several REST APIs, collect all the results, massage them and return a complete JSON to the client (HTML5 + AJAX).
- Call API A
- Call API B
- Call API A again (with results from B)
- Process results from the 3 calls into a JSON
- response.send(result)
I’m sure/hoping there’s an easy pattern, or solution, or module that I just didn’t google properly for 🙂
I’d also would appreciate an opinion on where to place such operations (under ‘routes’? Seperate files? etc.)
Thanks for your time!
The async module fits into this kind of job. Specifically, you may use the async.waterfall function.
Example:
Edit, if you have some nontrivial dependencies between the jobs, then you may use async.auto. It will determine the best order for running functions based on their requirements.