I’m learning about JavaScript’s Promise function and I expect to run the following in sequence:
- Do_System_Refresh(); –> In this function, I call the AJAX function within System_Refresh() for some purposes.
- Do_Refresh_UI();
I tried the following, but I encountered an exception: Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received.
async function Do_System_Refresh()
{
let p = new Promise(function(Do_Refresh) {
Do_Refresh_UI();
});
await System_Refresh();
}
async function System_Refresh()
{
call ajax here
}
function Do_Refresh_UI()
{
alert('here do refresh called');
}
What did I do wrong?
I need advice, many thanks in advance.
2
Answers
Why are you using promise in the async function Do_System_Refresh() if you are not even going to use it? You don’t need to use promise for the Do_Refresh_UI() function it is synchronous.
as for System_Refresh(), where the Ajax call is happening it already binds in the async function.
I hope you get the answer 🙂
And to answer the question:
The problem is that an exception occurs somewhere in one of your promises, but you neither wait for it with
await
nor use.catch(function)
to handle the error.In your example "Do_System_Refresh" you create a promise p, but never wait for it to complete. If any error occurs when the code is executed, the error is uncaught.
Go through your code and check that you are either waiting for the promise to complete with `await’ or returning the promise to the calling function.