DEV Community

Cover image for Tracking responses order in parallels requests and potential errors.

Posted on • Updated on


Tracking responses order in parallels requests and potential errors.

When submitting parallels requests, you are by definition unaware of the order of the eventual responses.

Furthermore, you need to know which request has eventually failed.

Rubico provides a nice way to resolve this problem.

In the following code, we launch four request toward 'jsonplaceholder'.
See how we can track each request by it's index.

Try to fecth a wrong url and see how the error is tracked.

You can test it here : TryCatch

const  { pipe, map, tryCatch } = rubico

const urls = [

const urlsIndexed = urls =>, index) => ({ url, index }));

const appDiv = document.getElementById('app');

appDiv.innerHTML = `
  <div style='display:flex'>
    <div style='width:70px;text-align:center'>index</div>
    <div style="width:100px;text-align:center">id</div>  
    <div style="width:300px;text-align:center">title</div>      
  </div></br />`;

const format_result= (index, id, title) => `
  <div style='display:flex'>
    <div style='width:70px;text-align:center'>${index}</div>
      <div style="width:100px;text-align:center">${id}</div>  
    <div style="width:400px;">${title}</div>  

const errorThrower = tryCatch(
  async ({ url, index }) => {
    let req = await fetch(url);
    let resp = await req.json();
    return format_result(index,, resp.title);
  (err, { url, index }) => {
    log('Error : ' + index);

function log(msg) {
  if (!msg) return;
  appDiv.innerHTML += `${msg}`;

map(pipe([errorThrower, log]))(urlsIndexed(urls));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await