最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Are promises lazily evaluated? - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question asked Feb 3, 2016 at 12:32 mjsmjs 65.4k27 gold badges96 silver badges129 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Yes, it is guaranteed. The specification of Promise has this step which will always be evaluated:

  1. 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).

发布评论

评论列表(0)

  1. 暂无评论