...would doFinalCheck() always be executed last?
Yes, unless doThis()/doThat() throws an exception. But keep reading.
If doThis() or doThat() are API requests and would take a bit of time to complete, would doFinalCheck() get executed before the API requests?
It depends on whether doThis() and/or doThat() are synchronous or asynchronous.
If they're synchronous, yes, onFinalCheck() will wait until they're done.
If they're asynchronous, onFinalCheck() will wait until the doThis()/doThat() call is done, but that call just starts the process. The process then continues and completes asynchronously. So onFinalCheck() will get called before the process is complete.
If you're dealing with asynchronous operations, look at promises and the new (ES2018) async functions.
For instance, if doThis()/doThat() are asynchronous and if they return a promise, then you can make onFinalCheck() wait for the operation to complete like this:
// I assume this is in an object initializer or `class`
async checkSomething(array){
// ^^^^^
if (array){
await doThis();
// ^^^^^
}
else {
await doThat();
// ^^^^^
}
doFinalCheck();
}
Like all async functions, checkSomething will return a promise.
Or if for some reason you can't use an async function, but you can use promises and doThis()/doThat() return promises, then:
// I assume this is in an object initializer or `class`
checkSomething(array){
return (array ? doThis() : doThat())
.then(result => {
doFinalCheck();
return result;
});
}
Note that checkSomething returns a promise above. If nothing is going to use that promise, then don't return and put an error check inline:
// I assume this is in an object initializer or `class`
checkSomething(array){
return (array ? doThis() : doThat())
.then(result => {
doFinalCheck();
})
.catch(error => {
// ...handle/report the error...
});
}