The only way the tests pass while the onFulfilled and onRejected functions are not invoked synchronously is when the onFulfilled and onRejected functions are delayed specifically with queueMicrotask.
While the linked spec for the exercise doesn't specifically mention queueMicrotask instead it mentions: setTimeout, setImmediate, proccess.nextTick which by the spec are valid ways of making sure onFulfilled and onRejected are not called synchronously:
https://promisesaplus.com/#point-67:~:text=Here%20%E2%80%9Cplatform%20code,handlers%20are%20called.
|
await waitForPromises(); |
|
const nextPromise = promise.then(onFulfilled, onRejected); |
|
it("`.then` onFulfilled gets called with resolved value", async () => { |
|
const { onFulfilled, resolvedValue } = await thirdSetup(); |
|
|
|
expect(onFulfilled).toHaveBeenCalledTimes(1); |
|
expect(onFulfilled).toHaveBeenCalledWith(resolvedValue); |
|
}); |
The only way the tests pass while the
onFulfilledandonRejectedfunctions are not invoked synchronously is when theonFulfilledandonRejectedfunctions are delayed specifically withqueueMicrotask.While the linked spec for the exercise doesn't specifically mention
queueMicrotaskinstead it mentions:setTimeout,setImmediate,proccess.nextTickwhich by the spec are valid ways of making sureonFulfilledandonRejectedare not called synchronously:https://promisesaplus.com/#point-67:~:text=Here%20%E2%80%9Cplatform%20code,handlers%20are%20called.
promises-training/src/tests/foundation/promise.test.ts
Lines 60 to 61 in 58d11d3
promises-training/src/tests/foundation/promise.test.ts
Lines 95 to 100 in 58d11d3