简体   繁体   English

解决未按预期工作的承诺

[英]Resolving a promise not working as expected

I want to use async / await to run console.log('Procccess Ends');我想使用 async / await 运行console.log('Procccess Ends'); after updateGuider function resolves.. updateGuider函数解决后..

Something like the code below:类似于下面的代码:

 tutor(); async function tutor(){ console.log('tutor function initiated..'); // wait until updateGuider function resolves await updateGuider('default'); // The expected result is to reach this line after updateGuider resolves, but we can't so far! console.log('Procccess Ends'); } function updateGuider(state){ return new Promise((resolve) => { if(state == 'done'){ console.log('updateGuider has been resolved!'); resolve(); } switch(state) { case 'default': speak(); break; } }); } async function speak(){ setTimeout(function(){ //after 5 seconds we resolve the updateGuider from speak function updateGuider('done') },5000) }

But even though we resolve the updateGuider it won't run the console.log('Procccess Ends');但是即使我们解决了updateGuider它也不会运行console.log('Procccess Ends');

What I miss and how to fix this?我想念什么以及如何解决这个问题?

How can I resolve updateGuider from speak ?我怎样才能解决updateGuiderspeak

UPDATE: Thanks to @h2ooooooo This code works but I can't understand how it works could you please give me a hand if it's a good solution and how it works!更新:感谢@h2oooooooo这段代码有效,但我不明白它是如何工作的,如果这是一个好的解决方案以及它是如何工作的,请帮我一把!

 tutor(); async function tutor(){ console.log('tutor function initiated..'); // wait until updateGuider function resolves await updateGuider('default'); // The expected result is to reach this line after updateGuider resolves, but we can't so far! console.log('Procccess Ends'); } function updateGuider(state){ return new Promise((resolve) => { switch(state) { case 'default': speak(resolve); break; } }); } async function speak(resolve){ setTimeout(function(){ //after 5 seconds we resolve the updateGuider from speak function console.log('entered speak') resolve(); },5000) }

you are returning different promises each time you call updateGuider.每次调用 updateGuider 时都会返回不同的承诺。 Strictly speaking, you cannot resolve updaterGuide like this.严格来说,你不能像这样解析 updaterGuide。 Also unless you await something in an async function it does nothing, so speak has currently no reason to be async.此外,除非您在 async 函数中等待某些东西,否则它什么都不做,所以目前没有理由是异步的。 This is not perfect, but you get the gist of the problem.这并不完美,但您已了解问题的要点。


function updateGuider(state){    
  return new Promise((resolve) => { 

    if(state == 'done'){
       console.log('updateGuider has been resolved!');
       resolve();    
    }

    switch(state) {
          case 'default':
          speak(resolve);
          break;         
    }

    });  
}

function speak(resolve){

   setTimeout(function(){
      //after 5 seconds we resolve the updateGuider from speak function
      resolve()    
   },5000)

} 

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

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