How Do I Resolve this 'Too Many Requests' Error in Node.js?

Viewed 20411

I'm trying to build a JSON file by making successive HTTP requests with Axios:

  1. Get an array of objects (projects)
  2. Create an array property in each project named attachments
  3. Get each project's tasks
  4. Get each task's attachments
  5. Push each project's task's attachments in to the project's attachments array
  6. Create a JSON file out of the modified projects array

Code:

let getProjects = function() {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/')
  } catch (error) {
    console.error(error)
  }
}

let getTasks = function(project) {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/'+project+'/tasks')
  } catch (error) {
    console.error(error)
  }
}

let getAttachments = function(task) {
  try {
    return axios.get('https://app.asana.com/api/1.0/tasks/'+task+'/attachments')
  } catch (error) {
    console.error(error)
  }
}

async function getAsanaData() {
  let projects = await getProjects()
  return Promise.all(projects.data.data.map(async (project) => {
      project.attachments = []
      let tasks = await getTasks(project.gid)
      return Promise.all(tasks.data.data.map(async (task) => {
        let attachments = await getAttachments(task.gid)
        project.attachments = !!attachments ? project.attachments.concat(attachments.data.data) : project.attachments 
        return project
      }))
  }))
}

getAsanaData()
.then((projects) => {  
  var asanaData = safeJsonStringify(projects);
  fs.writeFile("thing.json", asanaData);
})
.catch(err=>console.log(err))

But I'm running into this error:

status: 429,
statusText: 'Too Many Requests

I haven't found anything helpful yet for figuring out how to resolve it. What can I do?

2 Answers
Related