Asynchronous Module Definition specification, abbreviated as AMD, has been introduced in an attempt to define standards that facilitate asynchronous loading on client-side. It is largely inspired by Inversion of Control pattern, module in AMD could be seen as an equivalence of bean from IOC world.
The API of AMD concerns two methods named define and require that are designed to declare and to use an AMD module from client-side.
A module identified as alpha that exposes square method at runtime is defined thanks to define method.
The function literal passed to define method plays the role of a module-factory function.
The base idea behind module definition could be expressed in a short message:
When i need alpha and if it is not available yet, please execute factory function and assign the result to alpha
Each module definition declares:
- Module id.
- Module dependencies.
- Factory function to produce module object.
As the factory method is not executed immediately, multiple define blocks could be loaded into browser in an any order.
Execution of factory method (i.e. module object creation) is deferred until the module itself or one of its dependents (direct or non-direct) is required via require method.
Execution of require method consists of three phases:
- Loading missing module definitions
In this phase, missing definitions of required modules are fetched/loaded into browser with the mechanism defined in implementation of AMD compliant library.
- Resolving modules
In an order determined by dependency relations, factory method of each unavailable module is executed and the returned result is assigned to module id.
- Callback execution
Once all required modules are available, the callback method function(arg_1, arg_2,..., arg_m) is invoked.