2

I have been battling with code below. I actually want to know how to return the value of the inner object 'result' in the main function 'myfunc()'.

function myfunc(){

        con.connect(function(err){

            con.query(q,function(err){

                var result = 'mY Result';
            });

        });

    }

I just want to assign 'result' to 'myfunc()'. Thanks

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
ubakasi
  • 45
  • 4

1 Answers1

2

You could take the following approach:

// add async here to denote the method as awaitable to callers
async function myfunc(){

    // Wrap the inner logic with a promise. This allows you
    // return a result, when it is available
    return new Promise((resolve) => {

        con.connect(function(err){

            con.query(q,function(err){

                // Once the inner functions complete, and
                // a result is available, we return that
                // result to external code by passing the
                // result to the 'resolve' function like
                // so:
                var result = 'mY Result';
                resolve(result)
            });

        });
    })
}

Now depending on how your project is setup, you will usually have two options for how you call myfunc():

// Option #1
const queryResultOption1 = await myfunc()

// Option #2
myfunc().then(function(queryResult) {

    // Use queryResult here
})

Generally speaking, option #2 is the "older" way of doing things but offers more widespread support, where as option #1 is the "newer" way and tends towards code thats easier to read, reason about and work with, etc.

Update

Note that there are some constraints when using Option #2. For instance, you can only call the myfunc() with await keyword in the context of an async method.

In other words, this means you can't just call await myfunc() directly if the place you're calling it from is not denoted async. So for example, to use this method directly in say, a nodejs entry script, you'd need to do something like this:

(async function () {

    const result = await myfunc()

})()

Hope this helps!

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
  • Thanks @Dacre. I tried your option but I get 'await is only valid in async function'. It shows the message when I do 'console.log(await myfunc());' – ubakasi Aug 27 '18 at 01:08
  • Hi you're welcome - please see update with `async` keyword prefixing the method. hope this helps :-) – Dacre Denny Aug 27 '18 at 01:10
  • I really don't understand why I keep getting that error message 'await is only valid in async function' even after prefixing the function with the keyword `async`. Any suggestion please? – ubakasi Aug 27 '18 at 01:20
  • Ah, this will depending on the *context* that you're calling the `async` method from. If you're just trying to call `myFunc` directly, and not in the context of an `async` function then it'll give you that error. See my updated answer – Dacre Denny Aug 27 '18 at 01:27
  • Thanks @Dacre Denny. Your last update worked perfectly for me. – ubakasi Aug 27 '18 at 07:34