You're overdoing manually a lot of things that Promises already do for you: axios.get already returns a Promise, so there is no point in return a the response when it resolves and return a false when it rejects. A catch handler at the end of a Promise chain already handles all errors that may arise during the chain, so you don't need to catch every Promise.
I would do something like:
function doAPICall1(){ return axios.get('...'); } function doAPICall2(){ return axios.get('...'); } function handle(){ // in case you would use the api calls results. let firstResult = null; let secondResult = null; return doAPICall1() .then(res => {firstResult = res}) .then(() => doAPICall2()) .then(res => { secondResult = res; return [] }) }
I guess you will use the Api calls results for something. With the code above, you could consume the handle() function like follows:
function someSortOfController(){ handle().then(results => { console.log(results[0]); // first api call result console.log(results[1]); // second api call result }) .catch(err => { // here you will find any error, either it fires from the first api call or from the second. // there is *almomst* no point on catch before console.log(err); }) }
There, you will access any error, either it came from the first api call or the second. (And, due to how Promises work, if the first call fails, the second won't fire).
For more fine grained error control, you may want to catch after every Promise so you can add some extra logs, like:
function doAPICall1(){ return axios.get('...') .catch(err => { console.log('the error came from the first call'); throw err; }); } function doAPICall2(){ return axios.get('...') .catch(err => { console.log('the error came from the second call'); throw err; }); }
Now, if the first api call fails, everything will work as before (since you're throwing the error again in the catch), but you have more control over error handling (maybe the error returning from API calls is not clear at all and you want this kind of control mechanism).
Disclaimer
This answer doesn't answer why your code acts like it does. However, there are so much things wrong in your code, so I think providing you with an example about using Promises is more valuable.
doAPICall1, you can either return a promise as @ChouW mentioned, or maybe calldoAPICall2inside the.thenofdoAPICall1