ClickUp API in Node.js Promise.all() vs for loop with await

84 Views Asked by At

I want to delete all folders on ClickUp in bulk:

Here is the function that deletes a single folder:

async function deleteFolder(folderID) {
  try {
    await clickup.delete(`folder/${folderID}`);

    logger.log(
      "info",
      `DELETE folder: ${folderID} - Folder deleted successfully`
    );
  } catch (error) {
    logger.log(
      "error",
      `DELETE request to https://api.clickup.com/api/v2/folder/${folderID} failed. Error message: ${error.message}`
    );
  }
}

Here is the function that deletes all folders:

async function deleteAllFolders() {
  const teamID = await getWorkspaces();
  const spaceID = await getSpaces(teamID);
  const allFolders = await getFolders(spaceID);

  const promises = allFolders.folders.map(async (folder) =>
    deleteFolder(folder.id)
  );

  await Promise.all(promises);
}

This function uses the Promise.all(). When I call this function, only some of the folders are being deleted. Which folders get deleted seems to be random. I'm getting a 500 error for some of the folders.

Here is the error log I'm getting:

{"level":"error","message":"DELETE request to https://api.clickup.com/api/v2/folder/90110574307 failed. Response code: 500, response message: Request failed with status code 500","timestamp":"2023-10-08T18:35:39.431Z"}

However, when I use a for loop with await, everything works fine:

async function deleteAllFolders() {
  const teamID = await getWorkspaces();
  const spaceID = await getSpaces(teamID);
  const allFolders = await getFolders(spaceID);

  for (const folder of allFolders.folders) {
    await deleteFolder(folder.id);
  }
}

Why does the for loop work as expected, whereas the Promise.all() version doesn't? Does it have to do with rate limits, since Promise.all() does concurrent requests. The ClickUp API is limited to 100 requests per minute (in the free plan). But I'm only deleting 6 folders max.

Any help is appreciated.

1

There are 1 best solutions below

0
On

ClickUp API has a rate limit of 100 requests per minute and then by using Promise.all() function you can make concurrent requests for all the folders.

This is my example code.

const deleteAllFolders = async () => {
  const teamID = await getWorkspaces();
  const spaceID = await getSpaces(teamID);
  const allFolders = await getFolders(spaceID);

  const promises = allFolders.folders.map(async (folder) => {
    await new Promise((resolve) => setTimeout(resolve, 6000)); // Delay for 6 seconds    
    return deleteFolder(folder.id);
  });

  await Promise.all(promises);
}