Thoughts on How to Combine API Calls

What’s a good way to make multiple API calls within Observable?

See notebook example

I’d like to get some eyes on a method for making multiple API calls and combining the results. My thinking is that I can make an array of parameters and map across them to return data via d3.json, which returns an array of Promises that I can then combine together. As someone with limited JS experience, is this a good approach for combining API calls? Does something stand out as being hazardous or a long-term bug? Got a better way or thoughts on how to approach this within Observable? (recommended reading, perhaps :books:)

Always learning,
Zach

That’s a great way to do it.

If I were to write this myself, I’d to it a bit differently out of personal preference. I made a fork of your notebook that encompasses those changes here: How to Combine API Calls / Mythmon / Observable

Here’s the things I changed:

  • Use object destructuring for the parameters to the getFacts function, which makes it clearer what the parameters are for, and makes it easier to pass all the parameters at once.
  • Directly use the fetch API to load the data instead of using d3.json. This doesn’t really make a difference, but it gives you some more flexibility in the future if the server contract changes.
  • Take advantage of the await keyword to handle promises.
  • I’d collapse everything to be a little more compact, instead of breaking out the steps. This is entirely a style thing. I tend to prefer cells that complete a full “thought”. Analagous to paragraphs instead of sentences. I love that Observable gives us the ability to break things up into various sizes though.
2 Likes

Thanks so much! Updated notebook with your approach. Appreciate hearing your preferences.