Html HTML5 localStorage 是异步的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20231163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is HTML5 localStorage asynchronous?
提问by jas7
Is the setItem(key,value)
function asynchronous?
是setItem(key,value)
功能异步?
localStorage.setItem("key",someText);
回答by Ryan Nigro
Nope, all localStorage
calls are synchronous.
不,所有localStorage
调用都是同步的。
回答by paxdiablo
Actually. web storage is no longer part of the HTML5 core standard, it's been split off.
实际上。Web 存储不再是 HTML5 核心标准的一部分,它已被拆分。
The relevant (draft) specification can be found hereand 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) methodmust 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 updatedto value. If its previous value is equal to value, then the method must do nothing.
的setItem(键,值)方法必须首先检查是否有键/值对与给定键与该对象相关联的列表已经存在。
如果没有,则必须将新的键/值对添加到列表中,使用给定的键并将其值设置为值。
如果给定的键确实存在于列表中,并且其值不等于 value,则必须将其值更新为 value。如果其先前的值等于 value,则该方法不得执行任何操作。
In standards, words like must
, shall
and may
carry veryspecific meanings. That fact that it's talking about what the methodmust do means that the method itself must do it, not defer it to some later time.
在标准的,喜欢的话must
,shall
并且may
携带非常特定的含义。谈论方法必须做什么的事实意味着方法本身必须这样做,而不是推迟到以后的某个时间。
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.
这也符合常识。如果setItem
是异步的,则可以将项目设置为特定值,然后立即检索它,获取其先前的值。
There isa 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.
但是,这仅就写入长期存储的内容而言。最后一句要求访问相同存储对象的脚本需要同步查看事物。
回答by Scott
This question has already been answered. For anyone who comes to this in the future, javascript is synchronous by nature. Things are only async we they are specified to do so. Because of this, you can assume all things are synchronous unless they are specified to be async. This is with javascript, not frameworks written in Javascript. They at times break this practice. Like NodeJs.
这个问题已经有了答案。对于将来遇到此问题的任何人,javascript 本质上是同步的。事情只是异步的,我们指定他们这样做。因此,除非将它们指定为异步,否则您可以假设所有事物都是同步的。这是使用 javascript,而不是用 Javascript 编写的框架。他们有时会打破这种做法。像 NodeJs。