繁体   English   中英

无法解析函数内部的承诺

[英]Unable to resolving the promise inside of a function

这是我的一段原始代码,为什么我不能以这种方式解析指令函数?

实际上我曾经解决过这样的承诺,其他功能的相同代码以前工作过。

我无法在没有手的情况下找到原因和解决方案。

 var instructResolve; async function instruct(location, category){ switch(location) { case 'User_Was_Silent': console.log('Start!') await audioPlay(); console.log('After audioPlay await is done, then resolve instruct!') instructResolve(); break; } return new Promise((resolve) => { instructResolve = resolve; }); }; function audioPlay(source){ console.log('await audioPlay..') return new Promise((resolve) => { setTimeout(function(){ console.log('audioPlay resolved..') resolve(); }, 5000) }); } recognize(); async function recognize(){ await instruct('User_Was_Silent'); //After resolving instruct do stuff like logging some success message on the console console.log('Final log success!') }

recognize函数中,我正在等待instruct函数解析,然后我们执行诸如在控制台上记录一些成功消息之类的操作,但是由于由于某种原因recognize无法解析我们无法看到console.log('Final log success!')

更新:我有一个类似的代码,可以正常工作,没有任何问题,我已经实现了承诺并像上面的代码一样解决,但它有效!

 var updateGuiderResolve; function updateGuider(state, guide, lower){ console.log('updateGuider...') switch(state) { case 'default': stateReveal("default"); break; } return new Promise((resolve) => { updateGuiderResolve = resolve; }); } function stateReveal(state){ console.log('stateReveal...') setTimeout(function(){ speak(); }, 5000); } function speak(){ console.log('speak...') setTimeout(function(){ console.log('updateGuiderResolve...') updateGuiderResolve() }, 5000); } async function tutor(){ await updateGuider('default'); console.log('final...') } tutor()

我可以在代码库中看到 2 个问题。

一个是

instructResolve(); // It's not function, I think as it's only declared at first.

第二个是

await instruct('User_Was_Silent'); // This does not need to add await as it's already async function. Simply call instruct('User_Was_Silent'); and missing second param in this function.

 var instructResolve; async function instruct(location, category=null){ switch(location) { case 'User_Was_Silent': console.log('Start!') await audioPlay(); console.log('After audioPlay await is done, then resolve instruct!') break; } }; function audioPlay(source){ console.log('await audioPlay..') return new Promise((resolve) => { setTimeout(function(){ console.log('audioPlay resolved..') resolve(); }, 5000) }); } async function recognize(){ await instruct('User_Was_Silent'); //After resolving instruct do stuff like logging some success message on the console console.log('Final log success!') } recognize();

尝试像这样在 Promise 内部调用 instructResolve :

var instructResolve;
async function instruct(location, category){
 switch(location) {
   case 'User_Was_Silent':
   console.log('Start!')
   await audioPlay();
   console.log('After audioPlay await is done, then resolve instruct!')
   break;
 }

 return new Promise((resolve) => { 
    instructResolve = resolve;
    instructResolve();
 });
}

有一个这样的解决方案,但我想知道为什么问题的代码不起作用?

 var instructResolve; async function instruct(location, category){ return new Promise(async (resolve) => { switch(location) { case 'User_Was_Silent': console.log('Start!') await audioPlay(); console.log('After audioPlay await is done, then resolve instruct!') resolve(); break; } }); }; function audioPlay(source){ console.log('await audioPlay..') return new Promise((resolve) => { setTimeout(function(){ console.log('audioPlay resolved..') resolve(); }, 5000) }); } async function recognize(){ await instruct('User_Was_Silent'); //After resolving instruct do stuff like logging some success message on the console console.log('Final log success!') } recognize();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM