意外的解决不是一个功能

Unexpected Resolve is not a function

我有这个功能,我想这样等待它:

await triesPlay(blobSource, reference);

但出乎意料的是 returns 在这个 if 语句中到达 triesPlayResolve(); 时出错:

if(consecutive(exist)) {
   report.consecutiveTried.push([reference, c]);
   triesPlayResolve(); // reaching this produces the error
   return;
}

这里是错误:

函数如下:

function triesPlay(blobSource, reference) {
    let triesPlayResolve;
    int();
    async function int() {
        for(let c = 0; c < options.maxTry; c++) {
            if(consecutive(exist)) {
                report.consecutiveTried.push([reference, c]);
                triesPlayResolve();
                return;
            }
            await proccess();
            function proccess() {
                let proccessResolve;
                int();
                async function int() {
                    await Recognize(blobSource);
                    if(speechResult.simplify() === '') {
                        int();
                        return;
                    }
                    saveExpect(speechResult.simplify(), reference);
                    primaryExpects.push(speechResult.simplify());
                    proccessResolve();
                }
                return new Promise(resolve => { 
                    proccessResolve = resolve;
                });
            }
        }
        report.maxTried.push(reference);
        triesPlayResolve();
    }
    return new Promise(resolve => { 
        triesPlayResolve = resolve;
    });
}

这对我来说没有任何意义!!我怎样才能解决这个问题?为什么会这样?!

您正在调用函数 之前 您在函数末尾的 new Promise 实例化初始化变量。

但是所有这些都是完全不必要的 - async function 开箱即用,它们总是 return 一个隐含的承诺,当函数体中的代码 [=13] =]s,甚至(或:特别是)在 await 之后。您不需要任何这些内部辅助函数。

所以你可以简化为

async function triesPlay(blobSource, reference) { /*
^^^^^ */
    for (let c = 0; c < options.maxTry; c++) {
        if (consecutive(exist)) {
            report.consecutiveTried.push([reference, c]);
            return;
//          ^^^^^^^ this is enough!
        }

        while (speechResult.simplify() === '') {
            await Recognize(blobSource);
        }
        saveExpect(speechResult.simplify(), reference);
        primaryExpects.push(speechResult.simplify());
    }
    report.maxTried.push(reference);
}