Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
mikihiir
Helper III
Helper III

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 then not working ? 

 

code example ->

 

 image.png

 

Thanks in advance !

1 ACCEPTED SOLUTION
v-evelk
Microsoft Employee
Microsoft Employee

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
pbicvsupport@microsoft.com

View solution in original post

8 REPLIES 8
v-evelk
Microsoft Employee
Microsoft Employee

Hi,

 

Aync will not work for synchronous code because it is syntax error

image.png

Why thread must be blocked?

You called async function as sync and it creates a Promise that is async but in fact will be called immediatelly, but you called setTimeout inside that is asynchronous too.

 

Evgenii Elkin,

Software Engineer

Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

I want the thread to wait until promise return the result

private storage: ILocalVisualStorageService -> API which returns storage info.

 

Problem is, that thread is not waiting for promise result, it moves on. Here is the example code. 

image.png

v-evelk
Microsoft Employee
Microsoft Employee

It is how async functions work on the top level. They will wait for results only in their chain.

 

However, there are 2 solutions that may solve your question:

1. You can wrap your code in a promise on the top level.

2. You can wrap your code in an immediate async function like below:

 

(async () => {
   try {

      debugger;
      await this.getData();

      debugger;
   } catch (e) {}
})();

 

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

Thanks for replying.

 

Tho option 2. is not helping,  code is still not waiting for promise. Can u make an example of option 1 (code in a promise on the top level) ?

 

 

v-evelk
Microsoft Employee
Microsoft Employee

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
pbicvsupport@microsoft.com

I for one, still can't get this to work

I have created a new post (https://community.powerbi.com/t5/Custom-Visuals-Development/Can-t-get-async-wait-to-work-in-any-form...) enquiring about this.

 

I am thinking that either I am not pointing my custom viz correctly to esnext or I am putting the async logic in the wrong place...

Hopefully this post will clarify for SDK v3.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


My course: Introduction to Developing Power BI Visuals


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.