Skip to content

Instantly share code, notes, and snippets.

@baakeydow
Created May 16, 2021 00:33
Show Gist options
  • Save baakeydow/ac6911744596d21c24587b583e1f949f to your computer and use it in GitHub Desktop.
Save baakeydow/ac6911744596d21c24587b583e1f949f to your computer and use it in GitHub Desktop.
simple recursive function to paginate api results
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