0

Should the caller function (CR) be declared with async keyword and hence be an asynchronous function when the callee function (CE) is declared with async keyword and is an asynchronous function? Assume that there is no other asynchronous task carried out by CR apart from calling CE but CR has some logic that depends on the response received from CE.

For example, I have an apiUtils.js file which consists of a generic asynchronous function to make an API call. This function has the following signature:

export const sendApiRequest = async (apiParametersGoHere) => {
  try {
    const response = await axios(objectForAxiosAsParameter); // axios() can be replaced with fetch() or XMLHttpRequest(); the idea remains the same
      // processing of response
  } catch (error) {
      // processing of error
  }
}

I also have other files which import apiUtils.js file to send specific API requests. For example, there's a userApi.js file which imports apiUtils.js and has a function named doLogin() with the following signature:

export const doLogin = async (userLoginRelatedParameters) => {
  try {
    const response = await apiUtils.sendApiRequest(apiArgumentsGoHere);
    // processing of response
  } catch (error) {
    // processing of error
  }
}

Should the doLogin() function be declared with the async keyword and therefore be an asynchronous function? If yes, should all callers in the call stack be declared with async keyword and therefore be asynchronous functions (assume another function F invoking doLogin() function)? Why is it so?

P.S.: Please ignore determining the usage of async keyword for doLogin() function just based on the fact that the await keyword is used inside the function body or try/catch blocks are used. The async/await keywords are used for the purpose of demonstration only. You can also assume chaining of promises.

Srishti Gupta
  • 1,155
  • 1
  • 13
  • 30
  • 5
    If you wish to use the keyword `await` inside a function, we _must_ use the keyword `async` before the `function` keyword (or `()` with arrow functions) – evolutionxbox Jun 12 '21 at 11:12
  • 4
    You don't have to use `async`/`await`, you can also use normal promise chaining, but in general yes - if the function is meant to wait for the asynchronous operation, it becomes asynchronous itself and should return a promise. – Bergi Jun 12 '21 at 11:15
  • @evolutionxbox To remove any ambiguity, I have added a PostScript (P.S.) to the question. – Srishti Gupta Jun 12 '21 at 11:34
  • @Bergi I have modified the question to focus on whether the function should be asynchronous or not (ignoring the actual syntax used in the example). Thanks for your comment on the general behavior. – Srishti Gupta Jun 12 '21 at 11:39
  • @Srishti usage of try/catch with promises is also only possible with async/await. – evolutionxbox Jun 12 '21 at 11:41
  • @evolutionxbox True that `try`/`catch` block can be used with `async`/`await` keywords and not a `Promise` object. I had to use one of the syntaxes in the example to demonstrate things more clearly. However, my question is whether the caller function should be asynchronous if it does no other work apart from calling another asynchronous function and the caller should wait for the response received by the callee function? – Srishti Gupta Jun 12 '21 at 11:54
  • 1
    *"whether the function should be asynchronous or not*" - there is no choice. If the function "*has some logic that depends on the response received from CE*" it **is** asynchronous, full stop. It is impossible to do that synchronously. – Bergi Jun 12 '21 at 12:09
  • @Bergi What if the caller function CR calls an asynchronous function CE but CR does not depend on the response received from CE? An example could be a fire-and-forget API call. – Srishti Gupta Jun 12 '21 at 12:14
  • @Srishti See [Can I fire and forget a promise?](https://stackoverflow.com/q/32384449/1048572) for that - you probably still *should* wait for it as there might be errors. – Bergi Jun 12 '21 at 12:15

0 Answers0