-
-
Save petermenocal/a23fdba0a59b34b6262403a616ff9fa7 to your computer and use it in GitHub Desktop.
For tutorial: How to get resources from paginated REST API
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 * as Utils from '../utilities' | |
class PlanetsListTable extends Component { | |
componentDidMount() { | |
new Promise((resolve, reject) => { | |
Utils.getPlanets('https://swapi.co/api/planets', [], resolve, reject) | |
}) | |
.then(response => { | |
this.props.loadPlanetsSuccess(response) | |
}) | |
} | |
... | |
} |
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 axios from 'axios' | |
export const getPlanets = (url, planets, resolve, reject) => { | |
axios.get(url) | |
.then(response => { | |
const retrivedPlanets = planets.concat(response.data.results) | |
if (response.data.next !== null) { | |
getPlanets(response.data.next, retrivedPlanets, resolve, reject) | |
} else { | |
resolve(retrivedPlanets) | |
} | |
}) | |
.catch(error => { | |
console.log(error) | |
reject('Something wrong. Please refresh the page and try again.') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment