Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next tick override functionality does function will be called

I need to override the following code

Here the function will be executed in the next tick

req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) {
    setTimeout(fn, 5);
} : function (fn) { fn(); }; 

with this,

window.require.nextTick = function(fn) { fn(); };

Since the function will be called immediately, Does in this case it won’t be executed on next tick ?

like image 626
07_05_GuyT Avatar asked Sep 26 '22 18:09

07_05_GuyT


1 Answers

if I change the code to the second option whethaer It will be problematic and if yes why???

I don't recommend making that change, because it can be problematic, generally speaking. The documentation for require.nextTick (which appears just before the function definition) says:

Execute something after the current tick of the event loop.

Calling fn synchronously violates the specification that execution should happen "after the current tick". (See the end of my answer for a possible objection here.)

If you wonder why it may be a problem consider that RequireJS listens to DOM events. One thing that having a function like require.nextTick does is give a chance to event handlers to run. If you set require.nextTick to execute its function synchronously then you are not giving a chance to event handlers to run. In some cases this could cause RequireJS to cease working properly.

At this point, one might object the definition of nextTick is such that it is okay to have it call fn synchronously, because if setTimeout is not defined, then it does call fn synchronously:

req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) {
    setTimeout(fn, 4);
} : function (fn) { fn(); };

I think this is meant for unusual cases, not the run-of-the-mill situation where modules are loaded through HTTP requests asynchronously. In some cases, like perhaps embedded devices that lack the JS environment provided by browsers or Node.js, the only way to execute a piece of software that uses RequireJS is to load an optimized bundle that has gone through r.js and include require.js in the bundle. In a case like this, having nextTick call fn synchronously would be moot because, by the time RequireJS executes, all the modules have been loaded already.

like image 135
Louis Avatar answered Sep 29 '22 07:09

Louis