You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用await调用triesPlay函数时触发Unexpected Resolve is not a function错误的原因分析与解决方法

问题分析与修复方案

错误原因

你遇到的Unexpected Resolve is not a function错误,核心源于两个关键问题:

  1. triesPlayResolve未完成赋值就被调用
    你的代码里,最外层的int()调用是在return new Promise(...)语句之前执行的。此时triesPlayResolve仅被声明但未赋值——它的赋值操作是在Promise的回调函数里完成的。当int函数执行到triesPlayResolve()时,它的值还是undefined,自然会触发“不是函数”的报错。

  2. 嵌套函数重名引发的作用域混乱
    你在proccess函数内部又定义了一个同名的int异步函数,这会直接覆盖外层的int函数引用,导致逻辑执行完全偏离预期。而且内部的int函数还存在无终止条件的递归调用,进一步加剧了逻辑混乱。

修复后的代码

我帮你重构了代码,解决了上述问题,同时优化了异步逻辑的可读性:

function triesPlay(blobSource, reference) {
    return new Promise(async (triesPlayResolve) => {
        // 重命名外层函数,避免和内部函数冲突
        async function attemptPlay() {
            for(let c = 0; c < options.maxTry; c++) {
                if(consecutive(exist)) {
                    report.consecutiveTried.push([reference, c]);
                    triesPlayResolve();
                    return;
                }
                await processAttempt();
            }
            report.maxTried.push(reference);
            triesPlayResolve();
        }

        async function processAttempt() {
            // 用循环替代递归,直到得到有效识别结果
            while(true) {
                await Recognize(blobSource);
                const simplifiedResult = speechResult.simplify();
                if(simplifiedResult !== '') {
                    saveExpect(simplifiedResult, reference);
                    primaryExpects.push(simplifiedResult);
                    break;
                }
                // 结果为空则继续循环重试
            }
        }

        // 现在调用外层函数,此时triesPlayResolve已被正确赋值
        await attemptPlay();
    });
}

关键修复点说明

  • 调整Promise结构:直接在return new Promise()中使用异步回调,确保triesPlayResolve在调用业务逻辑前就完成赋值,从根源避免未定义问题。
  • 重命名函数避免冲突:将外层的int改为attemptPlay,内部处理函数改为processAttempt,彻底解决函数重名导致的作用域混淆。
  • 替换递归为循环:把原有的递归调用改为while(true)循环,直到获取非空的识别结果,逻辑更清晰,也避免了无限递归的风险。
  • 明确异步流程:所有异步操作都添加await,保证执行顺序完全符合预期。

内容的提问来源于stack exchange,提问作者Sara Ree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:52:40