诺言被懒洋洋地评估了吗? [英] Are promises lazily evaluated?
问题描述
下面的代码是否保证输出HERE
?
Is the code below guaranteed to output HERE
?
var p = new Promise(() => console.log("HERE"))
(也就是说,如果从未调用 p.then(…)
, var p = new Promise(fn)
是否总是执行 fn
用结果做些什么?)
(That is, does var p = new Promise(fn)
always execute fn
if p.then(…)
is never called to do something with the result?)
更具体地说,在服务人员的上下文中,如果我调用 Cache.delete()
但不要在返回值上调用 .then()
(或者我丢弃返回值),这样可以保证缓存项要删除吗?
More specifically, in the context of service workers, if I call Cache.delete()
but never call .then()
on the return value (or I throw away the return value), is the cache entry guaranteed to be deleted?
推荐答案
是的,可以保证. Promise
的规范具有以下步骤:将始终被评估:
Yes, it is guaranteed. The specification of Promise
has this step which will always be evaluated:
- 让完成作为Call(执行程序,未定义,«resolvingFunctions.[[Resolve]],resolvingFunctions.[[Reject]]»).
其中 executor
是您传递给 Promise
构造函数的内容,而
where executor
is what you passed to the Promise
constructor, and Call results in that code being run. This all happens before the Promise
is even returned to your p
variable.
这篇关于诺言被懒洋洋地评估了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!