Forum Discussion

mikihiir's avatar
mikihiir
Helper III
7 years ago
Solved

Custom Visual await Promise

Hei   Has anyone tried to await for Promise ? Seems that thread is not blocked  until Promise  returns the answer. it just runs on. Async is working perfectly but when using await for syncronous ...
  • v-evelk's avatar
    v-evelk
    7 years ago

    The second approach must work because it is regular JS behavior. PowerBi Custom Visual code cannot work other way than regular JS flow.

    I suppose that you probably put your code in a wrong place.

    I attached an example of the code that outputs the messages in the next order:

     

    "Async function was started"

    "Set Timeout"

    "Promise result was gotten"

    "Some code after async call!"

     

    <script>
    function getPosts() {
       return new Promise((resolve, redject) => {
          setTimeout(() => {
             console.log("Set Timeout");
             resolve(1);
          }, 2000);
       });
    }
    
    async function init() {
       console.log("Async function was started");
       await getPosts().then((result) => { console.log("Promise result was gotten") });
    }
    
    
    (async () => {
        try {
            var text = await init();
            console.log("Some code after async call!");
            // other code that must wait for async result must be located here
        } catch (e) {
        }
    })();
    </script>

     

    Evgenii Elkin,
    Software Engineer
    Microsoft Power BI Custom Visuals
    [email protected]