-1

I have two functions, the first is the main function which has switch statement calling the second which is async. If my second function is async isn't that non-blocking? Would I still have to make the first one async in order for it to be non-blocking, if so why?

Example

exports.funcOne = async (theParam) => { // async ??
    switch (theParam) {
        case 'hey':
            return await funcTwo()
       default:
            ...
    }
}

const funcTwo = async () => {
   await axios.get...
}
Garrett
  • 1,576
  • 4
  • 27
  • 51
  • 1
    async/await doesn't change what is and isn't blocking, it just pushes it off to the next tick. – Kevin B Oct 02 '19 at 15:40
  • @Jonas Wilms I don't think this is a duplicate of the question you marked it with. That question is a general question about Async / Await. This was asking about having to use two sets of async awaits in nested functions – Garrett Oct 02 '19 at 17:35
  • I already added an answer specific to your question. – Jonas Wilms Oct 02 '19 at 17:40
  • The question is not an exact duplicate, however the answers there will answer your question here. – Clint Oct 02 '19 at 18:26

3 Answers3

0

Second function can just return the promise :

exports.funcOne = async (theParam) => { // async ??
    switch (theParam) {
        case 'hey':
            const myData = await funcTwo();
            console.log("response from request : ", myData);
            //do something with data
       default:
            ...
    }
}

    const funcTwo = () => {
       //axios.get is a promise, so is the return type for funcTwo
       return axios.get...
    }

  • So as long as funcOne is async funcTwo doesn't have to be because it's already returning a promise and thus doing an async thing? – Garrett Oct 02 '19 at 15:19
  • This is a common bug to forget to RETURN the promise, if so "void" is return, and so the first await do not wait !. Sometime we forget async await is a beautifull solution thanks to Promise, and because javascript is not strongly typed, it allow to return implicitly the "void" thing if no return statement is done. – Éric Alvernhe Oct 02 '19 at 15:24
  • Do I still have to return inside funcOne? – Garrett Oct 02 '19 at 15:25
  • It is up to you : what is the type of return for funcOne ? You already await the result from functTwo, so i suppose you now want to deal with the response (maybe convert the json from your call into data ? In any case the promise is now fullfil, so no need to send it to an other level. So to be clear in my opinion no you just have to do someting with the returned value, not return it. I change the snipet with a new funcOne to show you me idear. – Éric Alvernhe Oct 02 '19 at 15:31
0

In general, all functions that contain await must be async.

That rule might make more sense if we imagine it would not exist, and if you could wait for an asynchronous result in a synchronous function.

First of all, we need to know one important things about functions:

They have to run to completion, or in other words: A regular function runs till it's return and no other code can run at the same time.

If we could await inside of a non-async function that would mean that we would block execution until the awaited promise resolves, as no other code can run in the meantime. As the code that resolves the promise also cannot run, we created a deadlock.

async functions only run to completion till they reach an await. Therefore, when axios.get(...) gets called, a promise gets returned synchronously from it, funcTwo halts execution, returns a promise itself, therefore funcOne also halts execution (thats possible cause it's async). Then the engine can continue with other stuff, and somewhen when the underlying request is done, the promise resolves, funcTwo continues execution which resolves the promise it returned, funcOne continues execution and resolves the returned promise too.

If my second function is async isn't that non-blocking?

That really depends on your definition of non-blocking. In general, every code of JS that executes kind of "blocks the thread" (as only one function can execute at a time), but as most tasks are very small, you won't notice that blocking.

An asnyc function is also blocking, as long as it doesn't halt for a promise to resolve. Then it is in a non-blocking waiting state.

Asynchronous tasks (e.g. doing a network request) also aren't blocking, cause they are handled by the engine (offloaded to other threads or hardware, etc.).

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Calling an AsyncFunction returns a Promise. As Eric said you could return the Promise in the funcTwo and it doesnt need to be an AsyncFunction because it is secuential and it is in the functionOne thread. So if you return the Promise in the functionTwo, the result of the Promise "axios.get..." will be resolved in "return await funcTwo()" in the functionOne.

Hector Martinez
  • 417
  • 4
  • 7