javascript - Are promises lazily evaluated? - Stack Overflow

Is the code below guaranteed to output HERE?var p = new Promise(() => console.log("HERE"))

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744714000a4589508.html

相关推荐

  • javascript - Are promises lazily evaluated? - Stack Overflow

    Is the code below guaranteed to output HERE?var p = new Promise(() => console.log("HERE"))

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信