0

Edit: Worth to say that is not duplicate, because in the marked duplicate; none of the functions have a child function inside. so returns from child don't return to parent.

--

I have an async function inside a IIEF. Like this

$(function($){
  asyncAction((result)=>{
    if(result == someValidation()){
      return false;
    }
  })
  console.log("don't execute this");
});

I need the return false to be executed for the parent IIEF function (it stops a global program flow)

However, there are 2 problems:

  1. Return false executes for child function, not for the parent (because it's a callback)
  2. Console.log executes because the asyncAction is not stopping anything.

How can I stop parent function to stop executing after the return false fron async child?

What I've tried:

$(function($){
  let resultToParent = true;
  asyncAction((result)=>{
    if(data == someValidation()){
      resultToParent = false;
    }
  })
  if(resultToParent == false){
    return false;
  }
});

But this does not work because the child async.

$(function($){
  try{
    asyncAction((result)=>{
      if(data == someValidation()){
        throw Error
      }
    })
  }
  catch(err){return false}
});

But this does not work because try/catch works for synchronous only? (not sure about this?)

-- Thanks for the help!

Emmanuel Orozco
  • 369
  • 1
  • 10
  • Worth to say that is not duplicate because, in the other example, there is no parent-child function. None of the functions have child functions inside. – Emmanuel Orozco Jul 10 '18 at 19:17
  • Simply move the code (`console.log("don't execute this");`) after the `if` statement in the callback. – Felix Kling Jul 10 '18 at 19:17
  • 1
    The problem is always the same, you just phrased it differently. If you read the duplicate you will learn how to restructure your code to make it work. – Felix Kling Jul 10 '18 at 19:17
  • Thanks for the answer @FelixKling, the problem is that even If I do that, the return from child does not apply to parent, so execution continues. – Emmanuel Orozco Jul 10 '18 at 19:25
  • All code that depends on the result of the async function call needs to be inside (or called from) the callback (and that's in the answers). You already have a callback so you are already half way there. – Felix Kling Jul 10 '18 at 19:26

0 Answers0