Created
May 16, 2021 00:33
-
-
Save baakeydow/ac6911744596d21c24587b583e1f949f to your computer and use it in GitHub Desktop.
simple recursive function to paginate api results
This file contains 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 axios from 'axios'; | |
const getAllResults = async (limit: number, offset: number, data: any[]): Promise<any[]> => { | |
const response = await axios.get('https://awesome.api.com/data/v1/get/money', { | |
timeout: 5 * 60 * 1000, | |
params: { | |
fields: "", | |
limit, | |
offset | |
}, | |
}) | |
if (response.data.length === 0) { | |
return data; | |
} else { | |
const results = data; | |
results.push(...response.data) | |
return await getAllResults(limit, offset + limit, results) | |
} | |
} | |
const allResults = await getAllResults(50, 0, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment