¿HTML5 localStorage es asíncrono?
Frecuentes
Visto 77,448 veces
2 Respuestas
238
Nope, all localStorage
calls are synchronous.
Respondido 11 ago 15, 15:08
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 negro
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. - El hombre muffin
Same for me, weird stuff - patotoma
I'm facing the same problem... its a weird behavior. - Daniel T Sobrosa
Same, I just added a timeout of 0 and it worked great. - serg06
72
Actually. web storage is no longer part of the HTML5 core standard, it's been split off.
The relevant (draft) specification can be found aquí 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):
EL 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
y may
llevar specific meanings. That fact that it's talking about what the Método 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.
Aún así, is a note at the bottom of the storage interface section which hints at the possibility of asynchronous behaviour:
Esta especificación no requiere que los métodos anteriores esperen hasta que los datos se hayan escrito físicamente en el disco. Solo se requiere coherencia en lo que ven los diferentes scripts que acceden a la misma lista subyacente de pares clave/valor.
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.
Respondido el 20 de junio de 20 a las 10:06
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
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
Stumbled on this and thought this was interesting to add to the must
, shall
, may
comment above RFC2119 ietf.org/rfc/rfc2119.txt - mistertee
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
@sookie The operating system should hide any hardware issues like that. Applications read from the operating system's buffer cache, not directly from disk. - senos
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas html or haz tu propia pregunta.
I shouldn't think so. Downvoter: what's your rationale? - Waleed Khan
Relacionado: stackoverflow.com/questions/17349584/… - megawac