Skip to content Skip to sidebar Skip to footer

Executing Asynchronous Function , Synchronously

I am making a chrome extension that blocks specific sites added to local storage through popup window. I think the problem in this code is, it returns before completion of chrome.s

Solution 1:

You can't call any asynchronous function in webRequest blocking listeners. There is no way around it, hacky or otherwise. It is fundamentally impossible, since JavaScript is single-threaded; an async operation won't even start before the current function finishes executing.

This means you cannot rely on asynchronous storage; you must have a local synchronous cache for it.

Save blocked_sites to a variable, and update it on chrome.storage.onChanged event. This way, you'll have a synchronous cache of the setting.

Solution 2:

With the way your code structured you won't be able to make return { cancel: matched }; execute after the callback. The reason is because return { cancel: matched }; is run in one execution frame ( the same that calls chrome.storage.local.get ) and the callback is run in some future execution frame. When you call asynchronous method it can only insert the callback into event loop for some future time, by the time that callback is executed return { cancel: matched }; is already executed.

Post a Comment for "Executing Asynchronous Function , Synchronously"