Skip to content

Instantly share code, notes, and snippets.

@ppaska
Created October 12, 2022 17:37
Show Gist options
  • Save ppaska/f9b7bf45c57515b5e4bed98fd6fc090e to your computer and use it in GitHub Desktop.
Save ppaska/f9b7bf45c57515b5e4bed98fd6fc090e to your computer and use it in GitHub Desktop.
Make a Sequential vs. Parallel API Calls in JSPython
from 'rxjs' import forkJoin, lastValueFrom, map
ids = [2, 7, 4, 9, 5]
async def run_Sequential():
data = []
for requestId in ids:
response = httpGet("https://jsonplaceholder.typicode.com/posts/" + requestId)
data.push({requestId, response})
return data
async def run_Parallel():
httpRequests$ = ids
.map(
requestId => httpRequest$("GET", "https://jsonplaceholder.typicode.com/posts/" + requestId)
.pipe(
map(r => {requestId, response: r.data})
)
)
return lastValueFrom(forkJoin(httpRequests$))
if __env.entryFunction == '':
return {
sequential: run_Sequential(),
parallel: run_Parallel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment