question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

await ModalService.CreateModalAsync doesn't wait for modal to be closed

See original GitHub issue

Hi next code doesn’t wait for modal to be closed, Console.WriteLine is executed as the modal is shown

modalRef = await ModalService.CreateModalAsync<MyComponent, DlgResult>(modalConfig, templateOptions);
  Console.WriteLine("after");

tried ModalService.CreateConfirmAsync but the same is there any alternative? thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zxyao145commented, Apr 18, 2021

Yes, you can create a custom proxy for ModalService as follow:

1. define a proxy service for ModalService

public class ModalServiceWrapper
{
	private readonly ModalService _modalService;

	public ModalServiceWrapper(ModalService modalService)
	{
		_modalService = modalService;
	}

	public Task CreateModalAsync<TComponent, TComponentOptions>(ModalOptions config,
		TComponentOptions componentOptions) where TComponent : FeedbackComponent<TComponentOptions>
	{
		var tsc = new TaskCompletionSource();
		config.AfterClose = () =>
		{
			tsc.SetResult();
			return Task.CompletedTask;
		};

		_ = _modalService.CreateModalAsync<TComponent, TComponentOptions>(config, componentOptions);
		return tsc.Task;
	}
}

2. Injection service ModalServiceWrapper

services.TryAddScoped<ModalServiceWrapper>();

3. Replacing the ModalService with the ModalServiceWrapper

@using OneOf;
@inject ModalServiceWrapper ModalService

<AntDesign.Button OnClick="ShowModal">Show parent</AntDesign.Button>

@code {
    async Task ShowModal()
    {
        var modalConfig = new ModalOptions();
        modalConfig.Title = "Basic Form";

        await ModalService.CreateModalAsync<MyComponent, MyOptions>(modalConfig,new MyOptions());
        Console.WriteLine("AfterHide");
    }
}

aside: MyComponent.razor

@inherits FeedbackComponent<MyOptions>

<h3>MyComponent</h3>

MyOptions

public class MyOptions
{
}
1reaction
zxyao145commented, Apr 5, 2021

Hi, @mrjohnr I’m sorry for my poor English, it’s possible to get result after modal closes using await, but there is no such consideration for the time being.

In my last reply, I said in the future that you can get results in OnOk’s callback or OnCancel’s callback function, it may appear in version 0.8 or version 0.7.4.

Now you can use the callback function of AfterClose. I’m Sorry for the bad development experience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - await bootstrap modal close by user
I want to prevent the code until the user has pressed the OK button on it. I tried async/await like this below, which...
Read more >
Async call on Close · Issue #53 · Blazored/Modal
I was able to solve this. private async void ModalClosed(ModalResult modalResult) { if (!modalResult.Cancelled) { UsersList = await UsersApi.
Read more >
javascript - await does not wait for Promise to finish
So it's a bit weird. The "waiting" happens within that aync function, not your initial js function. consider this example
Read more >
await operator - asynchronously wait for a task to complete
The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes.
Read more >
await - JavaScript - MDN Web Docs - Mozilla
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found