npm install --save applyq
How the Asynchronous Syntax Works
The asynchronous syntax uses a queue, which is a
first-in, first-out data structure
that collects API calls until the target object is ready to execute them.
To push an API call onto the queue, you must convert it from the traditional
arrays that conform to a certain format. The first element in a command array is
the name of the object method you want to call. It must be a string. The rest of
the elements are the arguments you want to pass to the object method. These can
The following code calls
logger.log using the traditional syntax:
The equivalent code in the asynchronous syntax:
window._loggerq = window._loggerq || ; _loggerq.push(['log', 'Hello world!']);
Once the object is loaded and initialized, it has to execute the queued command
arrays. This can be done by calling
var logger = new Logger(); applyq(logger, _loggerq);
After the object had been initialized, there is no need to store the commands
anymore. The commands can be executed right away. That's why after calling
push method of the commands queue is overridden. The overridden version of
push is executing the command arrays immediately instead of storing them.
Whitelist of methods
You might want to process a subset of commands only. In that case you can path the list of commands to process:
/* In this case only commands passed to the register() method will be processed */ applyq(server, _serverq, ['register']);
Some of the explanations of the Asynchronous Syntax were taken from Google Analytics Tracking Basics (Asynchronous Syntax).