Mozilla WebExtension API storage - Debugging with and without breakpoints leads to different output

回眸只為那壹抹淺笑 提交于 2019-12-04 18:44:41
Andrew Swan

storage.local.set() is asynchronous. The data isn't guaranteed to be visible to a call to get() until the callback is invoked (or if you use browser.storage.local, set() will return a Promise and the data will be guaranteed to be written when that Promise resolves).

Your code is inherently full of race conditions. The calls to addItem() and removeItem() are racing against the calls to visitItems(), by setting breakpoints you let one path or the other "win" the race.

If you expect deterministic results, I would suggest using the Promise variants of storage.local and writing your code as a series of chained handlers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!