192

Is the setItem(key,value) function asynchronous?

localStorage.setItem("key",someText);
Jason
  • 1,879
  • 16
  • 19
jas7
  • 2,893
  • 6
  • 23
  • 36

2 Answers2

252

Nope, all localStorage calls are synchronous.

Alex Chuev
  • 693
  • 5
  • 17
Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23
  • 5
    My original source was the Mozilla localstorage docs, but it looks like they've been revised since then (and the W3 spec doesn't appear to require sync/async anywhere). At this point, I'd say localstorage calls are synchronous by convention but not by spec. Unless you're aware of a browser that's implemented it async? – Ryan Nigro Jun 30 '17 at 12:57
  • 73
    my problem is that I set an auth token in local storage, and then redirect user to another view. Sometimes on the new view access local storage finds that the token wasn't saved yet, so I have to use a timeout, but still not 100% reliable. – The Muffin Man Oct 06 '17 at 16:34
  • @TheMuffinMan: Your problem is probably related to `nbf` or `exp` from the auth token. – Mendes Nov 10 '17 at 13:10
  • 2
    Same, I just added a timeout of 0 and it worked great. – serg06 Jul 25 '21 at 00:33
74

Actually. web storage is no longer part of the HTML5 core standard, it's been split off.

The relevant (draft) specification can be found here and the one thing you'll notice is that it doesn't mention synchronous or asynchronous anywhere.

However, analysis of the text would suggest that it must be synchronous (my bold):

The setItem(key, value) method must first check if a key/value pair with the given key already exists in the list associated with the object.

If it does not, then a new key/value pair must be added to the list, with the given key and with its value set to value.

If the given key does exist in the list, and its value is not equal to value, then it must have its value updated to value. If its previous value is equal to value, then the method must do nothing.

In standards, words like must, shall and may carry very specific meanings. That fact that it's talking about what the method must do means that the method itself must do it, not defer it to some later time.

This also defers to common sense as well. If the setItem were asynchronous, it would be possible to set an item to a specific value then immediately retrieve it, getting its previous value.


There is a note at the bottom of the storage interface section which hints at the possibility of asynchronous behaviour:

This specification does not require that the above methods wait until the data has been physically written to disk. Only consistency in what different scripts accessing the same underlying list of key/value pairs see is required.

However, that's only in terms of what's written to long-term storage. The last sentence mandates that scripts accessing the same storage object are required to see things synchronously.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    Indeed things are not necessarily flushed to disk at once, so if you close then open again your page, you may not have the latest elements you stored. I've tested that in a hybrid app in Android, and it makes the usage of localStorage inapropriate in some cases. – user276648 Apr 23 '15 at 13:38
  • 2
    i need to store some object which i have to in different page, the most appropriate is use `LocalStorage`. When i store 10 items, i miss 3 of them. I really believe that it's asynchronous, but how to check if it done. where i have to add callback? – Adi Prasetyo Jun 09 '17 at 14:05
  • 1
    Stumbled on this and thought this was interesting to add to the `must`, `shall`, `may` comment above RFC2119 https://www.ietf.org/rfc/rfc2119.txt – mistertee Mar 06 '18 at 14:22
  • 2
    I am also seeing asynchronous-like behavior. When I save an empty object to an item, I notice that when retrieving the item's data immediately afterwards returns the old data prior to saving. If I were to guess, it's probably just the delay of writing to disk – sookie Mar 19 '18 at 13:52
  • @sookie The operating system should hide any hardware issues like that. Applications read from the operating system's buffer cache, not directly from disk. – Barmar Apr 10 '19 at 12:52