[英]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.