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

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

当然我的逻辑要复杂得多,但这是一个占位符代码,我试图在其中停止递归调用,但 break 关键字显示 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 
    }
};

这不是普通的 javascript 而是打字稿,我的打字稿版本是:

tsc -v Version 3.7.5

我无法理解这个错误是什么意思以及为什么会出现,我在互联网上搜索但没有找到任何原因,过去几年我一直在使用 break 来打破循环,现在它显然开始了不工作并说一个我不明白的错误 如有任何帮助,我们将不胜感激。

您没有要中断的循环。您正在递归调用您的函数,这与循环不同。使用 return 而不是 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;
  }
};