Skip to content

Inconsistency: StorageArea callback order #292

@lapcat

Description

@lapcat

In Chromium and Firefox, StorageArea.get() and StorageArea.set() have FIFO behavior. In Safari, they do not.

The MDN documentation assumes FIFO:

// store the objects
browser.storage.local.set({kitten, monster})
  .then(setItem, onError);

browser.storage.local.get("kitten")
  .then(gotKitten, onError);
browser.storage.local.get("monster")
  .then(gotMonster, onError);

The sample code on MDN works as expected in Firefox:

OK content.js:2:11
Moggy has 2 eyes content.js:6:11
Kraken has 10 eyes content.js:10:11

But not in Safari:

OK (content.js, line 2)
Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'item.kitten.name') gotKitten (content.js:6)
Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'item.monster.name') gotMonster (content.js:10)

(It's not clear from the console log, but gotKitten and gotMonster are actually called before setItem.)

The Chrome documentation also seems to assume FIFO:

chrome.storage.local.set({key: value}, function() {
  console.log('Value is set to ' + value);
});

chrome.storage.local.get(['key'], function(result) {
  console.log('Value currently is ' + result.key);
});

If you use for example const value = "testing"; then the sample code works in Chrome:

content.js:4 Value is set to testing
content.js:8 Value currently is testing

But not in Safari:

Value currently is undefined (content.js, line 8)
Value is set to testing (content.js, line 4)

The Safari team has said that this works as designed, and there's no guarantee of order, but Safari is inconsistent with Chrome and Firefox, and Safari is broken with the instructional sample code for the API on MDN. This situation can be confusing and unexpected to extension developers.

Whatever the community decides about this issue, the documentation and sample code ought to be updated to make the behavior clear, specifying whether it's undefined or defined FIFO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions