Async messaging return true won't work

282 views
Skip to first unread message

Uğur

unread,
Sep 7, 2022, 6:32:33 AM9/7/22
to Chromium Extensions
Hello all,

I sent message to background js from my popup.

background js has listener and after getting message from popup,
calls another async method then calls sendResponse with result of the async method

but pop up js doesn't wait for the response.. Can you help?


err1.jpg

this is from background.js


err2.jpg
err3.jpg

pop up js.
err4.jpg

T S

unread,
Sep 7, 2022, 7:29:28 AM9/7/22
to Chromium Extensions, ugur.kell...@gmail.com
I suspect this doesn't work with await - the temporary port is liable to close if the listener pauses execution before returning true.  Try swapping await for a .then so that you can return true immediately?

Uğur

unread,
Sep 7, 2022, 8:57:55 AM9/7/22
to Chromium Extensions, T S, Uğur
err5.jpg
It didn't work either. I don't know what is the problem..

7 Eylül 2022 Çarşamba tarihinde saat 14:29:28 UTC+3 itibarıyla T S şunları yazdı:

Jackie Han

unread,
Sep 7, 2022, 9:01:30 AM9/7/22
to T S, Chromium Extensions, ugur.kell...@gmail.com
async function f() {
    return true;
}


async function return true? No, the function itself always returns a Promise (here is Promise<boolean>). Because it is not true, you encounter the problem. Below are some correct ways to do it.

function f() { // no async declare
    asyncFunc().then(value => sendResponse(value));
    return true;
}

function f() { // no async declare
    // pass sendResponse to another function to use. 
    // you already use this way, so remove the outer function's sync keyword and remove await keyword inside it.
    asyncFunc(sendResponse);
    return true;
}



--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/86af3243-4557-4189-8fc4-91aee808b7f6n%40chromium.org.

Robbi

unread,
Sep 7, 2022, 9:04:46 AM9/7/22
to Chromium Extensions, ugur.kell...@gmail.com, T S
Try a Long-lived connection . It works in 99% cases

Uğur

unread,
Sep 7, 2022, 3:14:49 PM9/7/22
to Chromium Extensions, Jackie Han, Chromium Extensions, Uğur, T S
Oh thank you, I missed that part. That works now
7 Eylül 2022 Çarşamba tarihinde saat 16:01:30 UTC+3 itibarıyla Jackie Han şunları yazdı:
Reply all
Reply to author
Forward
0 new messages