I am doing a code change to convert .then(func{}) style code to async await.
In my example, converting from then to async await, removes the ability to query the API in parallel and process them in the order the request finished as two requests are independent from each other.
Is this a valid difference between the two syntaxes or is it just a matter of breaking two functions into two individual async functions that will make them run in parallel?
Sample code before upgrade:
componentDidMount() {
this.loadLists();
}
loadLists() {
console.log('start 1');
api.get('/url/1').then(function(r) {
console.log('done 1', r.body);
});
console.log('start 2');
api.get('/url/2').then(function(r) {
console.log('done 2', r.body);
});
}
//OUTPUT
//start 1
//start 2
//done 1
//done 2
Sample code after upgrade:
componentDidMount() {
this.getLists();
}
async getLists() {
console.log('start 1');
var res = await api.get('/url/1');
console.log('done 1', res.body);
console.log('start 2');
var res2 = await api.get('/url/2');
console.log('done 2', res2.body);
}
//OUTPUT
//start 1
//done 1
//start 2
//done 2
EDIT:
If the functions are split into two, async loadList1(), async loadList2()
Is calling both functions without the word await a proper use, that will result in two requests being submitted (nearly) simultaneously?