简体   繁体   中英

Unable to resolving the promise inside of a function

Here is a piece of my original code, Why I can't resolve the instruct function this way?

Actually I used to resolve the promises like this and the same codes for other functions worked before.

I'm not able to find the reason and the solution without a hand.

 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!') }

At the recognize function I'm waiting for the instruct function to resolve and then we do stuff like logging some success message on the console, but since recognize for some reason doesn't resolve we can not see the console.log('Final log success!')

UPDATE: I have a similar code that works just fine without any issues, I have implemented the promises and resolves just like the code above but it works!

 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()

I can see 2 problems in the code base.

One is

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

Second one is

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();

Try calling instructResolve inside Promise like that:

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();
 });
}

There is a solution like this one but I want to know why the question's code doesn't work?

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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