A slice of time in an application. Provides hooks to allow
functions to be executed before, during and after, the defined slice.
Handlers can be registered to a phase using before()
, use()
, or after()
so that they are placed into one of the three stages.
var Phase = require('loopback-phase').Phase;
// Create a phase without id
var anonymousPhase = new Phase();
// Create a named phase
var myPhase1 = new Phase('my-phase');
// Create a named phase with id & options
var myPhase2 = new Phase('my-phase', {parallel: true});
// Create a named phase with options only
var myPhase3 = new Phase({id: 'my-phase', parallel: true});
Name | Type | Description |
---|---|---|
[id] |
String
|
The name or identifier of the |
[options] |
Object
|
Options for the |
Name | Type | Description |
---|---|---|
[id] |
String
|
The name or identifier of the Phase |
[parallel] |
Boolean
|
To execute handlers in the same stage in parallel |
Name | Type | Description |
---|---|---|
id |
String
|
The name or identifier of the |
options |
Object
|
The options to configure the |
Register a phase handler to be executed after the phase completes.
See use()
for an example.
Name | Type | Description |
---|---|---|
handler |
Function
|
Register a phase handler to be executed before the phase begins.
See use()
for an example.
Name | Type | Description |
---|---|---|
handler |
Function
|
Begin the execution of a phase and its handlers. Provide a context object to be passed as the first argument for each handler function.
The handlers are executed in serial stage by stage: beforeHandlers, handlers, and afterHandlers. Handlers within the same stage are executed in serial by default and in parallel only if the options.parallel is true,
Name | Type | Description |
---|---|---|
[context] |
Object
|
The scope applied to each handler function. |
callback |
Function
|
Name | Type | Description |
---|---|---|
err |
Error
|
Any |
Return the Phase
as a string.
Register a phase handler. The handler will be executed
once the phase is launched. Handlers must callback once
complete. If the handler calls back with an error, the phase will immediately
halt execution and call the callback provided to
phase.run(callback)
.
Example
phase.use(function(ctx, next) {
// specify an error if one occurred...
var err = null;
console.log(ctx.message, 'world!'); // => hello world
next(err);
});
phase.run({message: 'hello'}, function(err) {
if(err) return console.error('phase has errored', err);
console.log('phase has finished');
});