Created
May 7, 2020 18:19
-
-
Save johnnyferreiradev/837684e9028314776046df94832b863c to your computer and use it in GitHub Desktop.
Function that joins all the data of an endpoint with pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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