After using backbone for a couple of weeks I have realized that underscore defer ended up fixing many of the async issues I ran into regarding rendering various views. Can someone please help me understand exactly what underscore defer does and how is it different that $.ready() or other type of wait for dom to render functions. What are the down sides of using it ?
_.defer = function(func) { return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1))); };
Underscore. JS is a popular javascript based library which provides 100+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on.
Lodash and Underscore are great modern JavaScript utility libraries, and they are widely used by Front-end developers.
_. each is synchronous, and will only return after the function was executed on all items. If that. get is asynchronous, each won't help you with that.
Underscore. js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects. For Docs, License, Tests, and pre-packed downloads, see: https://underscorejs.org.
# These are equivalent _.defer(fn); setTimeout(fn, 1);
So defer
is simply a one millisecond setTimeout
. (It's got a few more convenience features but those aren't important here.)
JavaScript has run loops. It's single threaded, but its execution starts and stops based on events or timers. Each time your JS engine kicks on to run some code, it's starting one iteration of its run loop.
So what defer
does is say "run this code in the next run loop".
_.defer(function() { alert('after'); }); alert('before');
This alerts "before" and then "after". This is because the the current run loop concludes which alerts "before", and then right afterward a new run loop starts and runs the code the alerts "after".
So anytime you have code right here, but you want it to run code which occurs after this code first, then you would use defer
.
_.defer(functionToRunLast); functionToRunFirst();
This can be handy with the DOM. Sometimes you change it, but the changes don't parse or render immediately. At the end of the run loop, the browser catches up and parses and renders the DOM, then the next run loop starts and can interact with the newly rendered DOM.
(Exactly what scenarios cause this delayed DOM parse, I'm not sure, but I've noticed it in my own projects in the past.)
It is NOT a replacement for DOM ready. The next run loop may happen before DOM ready ever fires, don't confuse these concepts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With