Is the code below guaranteed to output HERE
?
var p = new Promise(() => console.log("HERE"))
(That is, does var p = new Promise(fn)
always execute fn
if p.then(…)
is never called to do something with the result?)
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?
Is the code below guaranteed to output HERE
?
var p = new Promise(() => console.log("HERE"))
(That is, does var p = new Promise(fn)
always execute fn
if p.then(…)
is never called to do something with the result?)
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?
2 Answers
Reset to default 6Yes, it is guaranteed. The specification of Promise
has this step which will always be evaluated:
- Let pletion be Call(executor, undefined, «resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]]»).
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.
As James said, it is guaranteed that the function will be called. Though this doesn't guarantee that the cache entry gets deleted!
You have to check the value of the promise resolution (true
if the cache entry is deleted, false
otherwise).