Skip to content

Calling an "asynchronous Javascript" from blazor interop #24492

@vd3d

Description

@vd3d

Is your feature request related to a problem? Please describe.

Currently, we can call the standard Javascript function from C# by using JSRuntime.InvokeAsync.

But, it does not work if it is an asynchronous method. For example, a case I cannot solve right now.
I try to know if the browser geolocation is already granted or not, to do this I wish to call the "Permission API".

The problem is that this "Permission API" is async, so my Javascript looks like this:

window.isGeolocGranted = async () => {
  permission = await navigator.permissions.query({ name: 'geolocation' });
  return permission.state == 'granted';
}

After long research, I didn't find any elegant solution to this issue.

Describe the solution you'd like

Should we simply call this async method with the same JSRuntime.InvokeAsync ?

What if the permissions API is blocking, by example, on some device it requests 4/5 seconds to wake up the geolocation,
then it requests authorization to the user (again 10 or more possible seconds to wait, he can even drink a cafe before
answering 'yes')!

Related idea

In Xamarin, we have Xamarin.Essentials... having Blazor.Essentials will be amazing.
Why ? Because it can be very complex, mainly to test on all the browsers, all the different cases etc.
Also, having something common, will help to improve the robustness of such modules ;-)

Good luck!

Metadata

Metadata

Assignees

Labels

DocsThis issue tracks updating documentationDoneThis issue has been fixedarea-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions