Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break says error: Jump target cannot cross function boundary. typescript

for sure my logic in much more complicated but here is a placeholder code where I am trying to stop a recursive call but break keyword says Jump target cannot cross function boundary .ts(1107)

let arr = [1, 2, 3, 4, 5, 6, 7, 8];

async function recCall(input: number[]) {

    if (input.length) {
        let eachItem = input.pop();

        // my logic includes http call with await

        recCall(input); // next iter
    }else{
        break; // says error 
    }
};

this is not plain javascript but typescript, my typescript version is:

tsc -v Version 3.7.5

I am unable to understand what does this error means and why it is occuring, I searched on the internet but didn't found any reasons, I have been using break for breaking loops past years and now it is apparently started not working and saying an error I do not understand any help would be appriciated.

like image 483
Inzamam Malik Avatar asked Oct 16 '22 07:10

Inzamam Malik


1 Answers

You don't have a loop to break from. You are calling your function recursively, this is not the same as a loop. Use return instead of break:

let arr = [1, 2, 3, 4, 5, 6, 7, 8];

async function recCall(input: number[]) {

  if (input.length) {
    let eachItem = input.pop();

    // my logic includes http call with await

    recCall(input); // next iter
  }else{
    return;
  }
};
like image 194
feedy Avatar answered Oct 19 '22 01:10

feedy