How to skip a promise that doesn't work?

Viewed 1276
async function getData() {
  const getProject = await axios.get('url', {
    auth: {
      username: 'username',
      password: 'pw'
    }
  });

  const projects = getProject.data.value;
  const promises = projects.map(project =>
    axios.get(`url`, {
      auth: {
        username: 'username',
        password: 'pw'
      }
    })
  );

  const results = await axios.all(promises)

  const KPIs = results.map(v => v.data.value);
}

What I am trying to do is:

  1. get an axios call to fetch the name of the projects.

  2. Use those fetched names to call multiple axios calls. This is supposed to give me some data from that project.

When I do the second axios call, there are some API calls that won't work because the data does not exist in that project. This was intended. But it will break by giving me the 404 error and won't reach the line const results = await axios.all(promises).

I want it to skip that whenever it doesn't exist and only call it from the one that exists and store the fetched data into KPIs. How can I do that?

EDIT

The first axios call returns an array of objects

ex) {id: "id...", name: "Javelin", url: " .visualstudio.com/_apis/projects/6a93eab2-4996-4d02-8a14-767f02d94993", state: "wellFormed", revision: 99, …}

Something like that. So, I literally just get the .name of this object and put it into my url like:

axios.get({id}.extmgmt.visualstudio.com/_apis/ExtensionManagement/InstalledExtensions/{id}/..../${project.name}_test/Documents

3 Answers

This wraps the call in a promise that won't fail.

You also need to consider than you probably want to filter out null responses.

async function getData() {
    const getProject = await axios.get('url', {
        auth: {
            username: 'username',
            password: 'pw'
        }
    });

    const projects = getProject.data.value;
    const promises = projects.map(fallible);

    const results = await axios.all(promises)

    const KPIs = results.filter(v => v).map(v => v.data.value);
}

function fallibleCall(project) {
    return new Promise((resolve, reject) => {
        axios.get(`url`, {
            auth: {
                username: 'username',
                password: 'pw'
            }
        }).then(resolve).catch(resolve);
    })
}

If the problem is with fetching project data you just need to handle errors that occur there, one way is to simply catch the error and return null instead.

const promises = projects.map(project =>
    axios.get(`url`, {
      auth: {
        username: 'username',
        password: 'pw'
      }
    }).catch(err => {
        return null;
    })
  );

You can easily fix that by handling 404 responses by yourself and keep rejecting for all other errors as follows:

const promises = projects.map(project =>
  axios.get(`url`, {
    auth: {
      username: 'username',
      password: 'pw'
    }
  }).catch(err => {
    if (err.response.status === 404) {
      return null; // or an empty array or whatever you want
    }
    throw err;
  });
);

See https://github.com/axios/axios#handling-errors for details.

Related