0

Here is one example function.

async function getRandomBig() {
    let result;
    result = await randomModule.getRandom();
    if (result > 0.9 ) {
        return getRandomBig();
    } else {
        return result;
    }
}

So Obviously, I would like that execution to randomModule.getRandom() happens asynchronously. Is this example the way to go?

Also, I would like to know how to make the first call to getOutput be asynchronous.

Thank you

4
  • 1
    entirely depends on the code in randomModule.getRandom Commented Apr 9, 2018 at 23:25
  • Thank you, let's say I create Math.random() inside that module, how to return the value asynchronously? Commented Apr 9, 2018 at 23:27
  • And also, is it possible to call getRandom() asynchronously even though the code gets executed synchronously? Commented Apr 9, 2018 at 23:28
  • 1
    you can wrap it by a promise Commented Apr 9, 2018 at 23:29

1 Answer 1

2

It's a little hard to answer your question given the info you've provided, but maybe this will help:

The async function will return a promise to the outside caller. Than means that results obtained from await will happen asynchronously. For example consider this code:

// normal synchronous function
function changeRes() {
  return "after"
}

// sync function that `awaits` it's result
async function getget() {
  res = await changeRes()
}

let res = "before"

// p will be a pending promise
var p = getget()

// what is res at this point?
console.log(res) // still "before" because await doesn't return until next tick

// now res is 'after'
p.then(() => console.log(res))

But be careful because the call to changeRes is not asynchronous — it is called in the current loop. Compare this with the first code. We only change the behavior of changeRes():

function changeRes() {
  res = "after"
  return res
}
async function getget() {
  res = await changeRes()
}
let res = "before"

// p is still a pending promise
var p = getget()

// but res was changed in changeRes synchronously
console.log(res)

p.then(() => console.log(res))

EDIT based on comment:

With the recursive function, everything important is happening inside the async function, so it all should work as expected. For example, you can substitute your randomModule.getRandom with the regular Math.random() which is synchronous, but the await will make it work in the context of an async function. So this function will return a promise that resolves to a random float less than 0.25:

async function getRandomBig() {
  let result;
  result = await Math.random();
  if (result > 0.25) {
    return getRandomBig();
  } else {
    return result;
  }
}

getRandomBig().then(console.log)

So will this even though it's truly async:

function asyncRandom(){
    return new Promise(resolve => {
        setTimeout(() => resolve(Math.random()), 500)
    })
}
async function getRandomBig() {
    let result;
    result = await asyncRandom();
    if (result > 0.25 ) {
        console.log("too big recurse")
        return getRandomBig();
    } else {
        return result;
    }
}
getRandomBig().then(console.log)

Sign up to request clarification or add additional context in comments.

3 Comments

I just saw that I had a typo in my question, can you update your question to reflect the recursion? thank you
Also to clarify, the function is called synchronously but the result is saved to the variable after the next tick, and than it continues
Great! Thank you. I really understand it now.

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.