簡體   English   中英

如何使一個函數在angular2中等待另一個函數完成執行

[英]How to make a function wait for another function to finish excecution in angular2

我需要獲得用戶輸入才能繼續執行功能,如何使函數等待waitForUserInput()。

如果我可以使用可觀察對象解決此問題,誰能幫助我將可觀察對象集成到組件內部定義的簡單函數中

這就是我卡住的地方

 myFunction(){
    doSomeOperation();
    result=waitForUserInput(); // Here a prompt will be shown, 
    if(result==1)
    {
    operation1();
    }
    else if(result==2)
    {
    operation2();
    }
    else if(result==3)
    {
    operation3();
    }
 }
// This function will show a prompt and user can input values    
 waitForUserInput(){ 
    showUserInputPrompt();
    if(input==someValue1)
    {
    return 1;
    }
    else if(input==someValue2)
    {
    return 2;
    }
    else if(input==someValue3)
    {
    return 3;
    }
 }

在這里,我需要等到waitForUserInput()完成后再繼續

那不可能 您無法在瀏覽器中暫停功能。 在瀏覽器中,只有一個UI線程。 如果您停止它,則在您的應用程序中將不再發生任何事情,因此完全不支持它。

您需要選擇其他策略才能獲得想要完成的目標。

如果您的被調用函數允許回調,則可以執行以下操作=>

this.instance.saveEditData()。done(this.CustomSave);

函數CustomSave(){}

CustomSave是父項,我在其中調用了saveEditData。 但是將邏輯更改為僅在saveEditData完成時才調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM