Comments (6)
These are async functions, when you await
anything in them even if that promise is already fulfilled or it's not even a promise it will defer a microtick before continuing to run.
The reason your bluebird example sometimes "works" is that bluebird internally knows some functions are supposed to be async like setTimeout so it skips that microtick which is done here: https://github.com/petkaantonov/bluebird/blob/master/src/timers.js#L32 and subsequently the microtick is skipped here: https://github.com/petkaantonov/bluebird/blob/master/src/promise.js#L714-L717
Basically bluebird is making the assumption setTimeout is async which is fair and the mocks break that assumption which messes with timing. A little like if you do Promise.setScheduler(fn => fn())
.
This is not a bug in fake-timers and there is nothing we can do to make the native promises behave like bluebird anyway - you need to await
those function calls :)
from fake-timers.
@benjamingr I'm not sure it's just a bluebird caveat. I show in my above example me defining my own util (i.e. not using bluebird), and observing similar odd behavior. I can look into maybe starting a docs PR, though I'm not sure I'll be able to explain the subtleties of tick execution as well as you guys can, but I can take a crack at it regardless.
function delay<T>(ms: number, value?: T): Promise<T | undefined> {
return new Promise(($resolve, $reject) => {
setTimeout(() => {
$resolve(value);
}, ms);
});
}
from fake-timers.
All good then, I suppose 😸?
from fake-timers.
Oh weird. Yeah, not all good :P didn't realize that mentioning this PR in our internal repo would close the issue... re-opening
from fake-timers.
Thanks for the explanation! It might be beneficial to add some notes to the documentation for the sync methods to warn folks of edge cases like this they might run into. It was very surprising and non-obvious for us, and a helpful doc caveat could have saved a lot of time.
Thanks for the great work on this lib!
from fake-timers.
Thanks for the explanation! It might be beneficial to add some notes to the documentation for the sync methods to warn folks of edge cases like this they might run into. It was very surprising and non-obvious for us, and a helpful doc caveat could have saved a lot of time.
This is more of a bluebird caveat than a fake-timers one, though I agree a note about this potential behavior may be useful. Wanna open a docs PR :)?
from fake-timers.
Related Issues (20)
- Test failing since Node 18.8.0 HOT 2
- The `shouldAdvanceTime` option seems to cause Jest Test environment to clear during the test HOT 4
- `runToLastAsync` doing infinite loop when `now` is specified to setup clock
- [Feature] Jump forward in time to simulate throttled timers HOT 22
- requestAnimationFrame is passed incorrect argument HOT 8
- [Feature]: Speed up all fake timers HOT 12
- [Feature]: In Node, hijack perf_hooks.performance in addition to global.performance HOT 5
- TypeError Since Node 19 HOT 4
- NodeJS "timers" module is not mocked HOT 17
- Mock "timers/promise" module HOT 2
- NodeJS `timers` module mock causing tests to timeout in JSDom HOT 3
- process.hrtime gives incorrect results HOT 4
- Leave global scope untouched HOT 5
- How should `shouldAdvanceTime` work together with `Date`? HOT 1
- runAllAsync and runToLastAsync do not run code in the microtask queue HOT 5
- Cleanly switch to real time and back again to fake time while testing HOT 2
- Skypack bundle cannot be loaded in the browser HOT 5
- `install({ toFake: ["setImmediate"] })` fails when `setImmediate` is not available in context (such as browser environment) HOT 5
- Support performance.measure method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fake-timers.