0

If I have a simple conditional in my function (in the example below), would doFinalCheck() always be executed last? If doThis() or doThat() are API requests and would take a bit of time to complete, would doFinalCheck() get executed before the API requests?

checkSomething(array){
  if (array){
    doThis();
  }
  else {
    doThat();
  }
  doFinalCheck();
}  
2
  • 2
    Depends on how doThis and doThat are implemented. And if they throw an exception. There's no general answer other than "it depends": implementation is everything. Commented Jul 16, 2018 at 16:15
  • There is start and end of execution of a function. If this/that doesn't do anything asynchronously, it's guaranteed order this -> that -> final. But if they do, there's no guarantee that this or that will finish sooner than final. Commented Jul 16, 2018 at 16:17

2 Answers 2

2

...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...
      });
}
Sign up to request clarification or add additional context in comments.

Comments

0

If doThis() and doThat() are async (such as network requests), then doFinalCheck() will execute immediately after the network requests are initiated, but before the request actually finishes.

If they are async, you'll need them to return a Promise (or have a callback). You can also use the await and async syntax (which is just syntactical sugar on the Promise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.