2017-08-22 125 views
0

我想在下面的代碼中使用async/await。JS調用嵌套的異步函數

if/else部分,它驗證正確,但嵌套函數不會被調用。

該代碼僅在if/else完成後停止。

難道有人會對此發光嗎?謝謝。異步功能

if (q1.value === 'yes') { 
     await askQuestion() 
    } else { 
     await endConvo() 
    } 

因爲askQuestion()和endConvo()是異步的功能,在程序退出之前

async function initBot() { 

    await botui.message.bot({ 
    content: `Do you have any questions?` 
    }) 

    let q1 = await botui.action.button({ 
    action: [{ 
     text: 'Yes', 
     value: 'yes' 
    }, { 
     text: 'No', 
     value: 'no' 
    }] 
    }) 

    // This is where it's stuck 

    if (q1.value === 'yes') { 
     askQuestion() 
    } else { 
     endConvo() 
    } 

    let endConvo = async() => { 
    await botui.message.bot({ 
     content: `END` 
    }) 
    } 

    let askQuestion = async() => { 
    await botui.message.bot({ 
     content: `What's your questions?` 
    }) 
    } 
} 

export default initBot 

回答

0

嘗試等待,而不必等待他們完成

+0

對我不起作用 – shawnl

+0

函數未被調用? – marvel308

0

啊啊呀。

它在if/else使用它之前宣佈這些功能endConvoaskQuestion的只是如此。

使用ESlint給了我這個線索。