Code address: try-tapable

Synchronous hooks

  • SyncHook: Synchronization hook
  • SyncBailHook: A callback registered during execution stops when it returns non-undefined
  • SyncWaterfallHook: Waterfall execution, taking at least one argument. The return value of the last registered callback is used as the argument of the next registered callback
  • Similar SyncLoopHook:SyncBailHook, but continues to execute the current callback here if the callback returns non-undefined during execution

Asynchronous hooks

  • AsyncParallelHook: The execution of subsequent logic in the final callback after all asynchronous tasks have finished; This hook can be executed in two ways:callAsync,promise; Note that when usedpromiseWhen neededreturn;
//***** src/Car.js *****
return this.hooks.calculateRoutesPromise.promise();

//***** src/index.js *****
car.hooks.calculateRoutesPromise.tapPromise('calculateRoutesPlugin22'.() = > {
    return new Promise((resolve, reject) = > {
      setTimeout(() = > {
        log('Calculate route - promise');
        resolve();
      }, 4000);
    });
  });
  
  car.calculateRoutesPromise().then(() = > log('Final callback - Promise'));
  
Copy the code
  • AsyncParallelBailHook: This hook has the same functionAsyncParallelHookSimilar, but will happen after the first plug-in registers the hook executionbail(Fuse), which is based on the plug-in’s registration time, not the plug-in’s execution completion time, and then invokes the final callback, regardless of whether the other plug-in has completed execution.
  • AsyncSeriesHook: Plug-ins execute sequentially
  • AsyncSeriesBailHook: Executes sequentially, calling the final callback as soon as one of the plug-ins returns a value, and not continuing with subsequent plug-ins
  • AsyncSeriesWaterfallHook: Executes sequentially, and the return value of the first plug-in is taken as an argument of the second

Hook type

Hooks are divided according to the execution logic of the registered plug-in

  • Basic hook. The registered plug-ins are executed sequentially. Such as SyncHook, AsyncParallelHook, AsyncSeriesHook.
  • Waterfall flow hook. The return value of the first plug-in is the input parameter of the second plug-in. Such as SyncWaterfallHook, AsyncSeriesWaterfallHook.
  • Bail hook. The Bail hook is a plug-in that returns a value other than undefined and then does not proceed with the execution of subsequent plug-ins. The Bail is one of the uygurs. For example: SyncBailHook, AsyncSeriesBailHook
  • Loop hooks. Loop through the plug-in until its return value is undefined. Such as SyncLoopHook.

To distinguish hooks in chronological order

  • Synchronous hooks. Sync hook
  • Asynchronous serial hooks. The hook at the beginning of AsyncSeries.
  • Asynchronous parallel hooks. AsyncParallel begins with a hook.