By default, endpoints that return an array of results return a Collection object. This object includes a nextPage() method used to fetch subsequent pages of results.
Requirements for pagination:
- You must provide a
limit query parameter in your initial request. - The
limit value must be between 1 and 100.
If you attempt to request an endpoint with more than 1000 results without pagination, you may receive the error: "The result is too large. You should use pagination (may require specifying a workspace)!"
const Asana = require('asana');
let client = new Asana.ApiClient();
client.authentications.token.accessToken = '<YOUR_ACCESS_TOKEN>';
let tasksApiInstance = new Asana.TasksApi(client);
let opts = {
"project": "<YOUR_PROJECT_GID>",
"limit": 100,
};
async function getAllTasks(opts) {
let tasks = await tasksApiInstance.getTasks(opts).then(async (response) => {
let result = [];
let page = response;
while(true) {
// Add items on page to list of results
result = result.concat(page.data);
// Fetch the next page
page = await page.nextPage();
// If there is no data in the next page break from the loop
if (!page.data) {
break;
}
}
return result;
}, (error) => {
console.error(error.response.body);
});
console.log('Tasks: ' + JSON.stringify(tasks, null, 2));
}
getAllTasks(opts);