When are Promise.then () hooks called?

I am seeing a delay in full-text notifications in Firefox promises. The following statement fails because onFullfilled () is called too late * .

var resolved = false; function onFullfilled() { resolved = true; log("Completed"); } Promise.resolve(true).then(onFullfilled); assert(resolved, "Promise completed promise should call resolution hook immediately."); 

When exactly is onFullfilled () guaranteed to be called when Promise resolves?

* In my case, the "Completed" error message appears after the confirmation of the frame analysis report fails.

+1
javascript promise es6-promise
source share
2 answers

Permission hooks are always called after all synchronization code has been executed. This is by design - and is done to prevent race conditions.

Since promises sometimes allow specifications to be specified asynchronously, they are always resolved asynchronously, so the same code path is executed. guard promises against Zalgo .

It is indicated here :

onFulfilled or onRejected should not be called until the execution context stack contains only platform code.

Many test frameworks, namely Mocha, support testing promises directly with the syntax of promises - returning a promise.

 it("does something", function(){ return aPromise; // if aPromise resolves the test passes }) 
+3
source share

You should always give a "next" function. Thus, you should use "onFullfilled ()" instead of "onFullfilled" as the parameter "then".

That is how it should be:

Promise.resolve (true) .then (onFullfilled ());

-2
source share

All Articles