- I used es6 async await function. Inside that, You can use Promise.all to resolve the all promises.
fetchData = async () => {
const a = new Promise((resolve, reject) => {
setTimeout(() => { console.log('one'); return resolve("done!"); }, 1000)
});
const b = new Promise((resolve, reject) => {
setTimeout(() => { console.log('two'); return resolve("done!"); }, 2000)
});
return await Promise.all([a, b]) ? true : false;
}
- Write the logic in componentDidMount instead of componentWillMount. you can use fetchData function as a
promise. After that you can set state whatever you want.
componentDidMount() {
this.setState({ loading: true })
this.fetchData().then((result)=>{
console.log(result, 'three');
this.setState({ loading: false });
});
}
You can see working example click here