Javascript 意外解析不是一个函数
我有这个功能,我想像这样等待它:Javascript 意外解析不是一个函数,javascript,es6-promise,Javascript,Es6 Promise,我有这个功能,我想像这样等待它: await triesPlay(blobSource, reference); 但出乎意料的是,它在到达triesPlayResolve时返回了一个错误;在该if状态内: if(consecutive(exist)) { report.consecutiveTried.push([reference, c]); triesPlayResolve(); // reaching this produces the error return; }
await triesPlay(blobSource, reference);
但出乎意料的是,它在到达triesPlayResolve时返回了一个错误;在该if状态内:
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;
});
}
这对我来说毫无意义!!我怎样才能解决这个问题?为什么会这样 在函数末尾的新Promise实例化初始化变量之前调用函数 但是所有这些都是完全不必要的-异步函数以这种方式工作,它们总是返回一个隐式承诺,当函数体中的代码返回时,甚至是:或者:特别是在等待之后,该承诺将得到解决。您不需要任何这些内部助手函数 因此,您可以简化为
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);
}
为什么你要在Promis的上下文之外获取resolve回调?你的ode肯定需要一个重构,因为你真的很难做到这一点。