Skip to content

Instantly share code, notes, and snippets.

@johnnyferreiradev
Created May 7, 2020 18:19
Show Gist options
  • Save johnnyferreiradev/837684e9028314776046df94832b863c to your computer and use it in GitHub Desktop.
Save johnnyferreiradev/837684e9028314776046df94832b863c to your computer and use it in GitHub Desktop.
Function that joins all the data of an endpoint with pagination
// import api from 'axios'; // axios is required
function getAllData(page = 1, dataList = []) {
return new Promise((resolve, reject) => {
api.get(`/endpoint/`, {
params: {
page, // Current pagination page
},
}).then((response) => {
const newPage = page + 1;
const newDataList = dataList.concat(response.data.results);
// Next represents the next page of the pagination
if (response.data.next) {
resolve(getAllData(newPage, newDataList));
} else {
resolve(newDataList);
}
}).catch((e) => {
reject(e);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment