lynx: requireModuleAsync() static method

Used to import modules. Just like the requireModule method, but loading asynchronously.

During the loading, the other JavaScript code can be executed.

The module loaded by requireModuleAsync needs to use a specific format. Use @lynx-js/runtime-wrapper-webpack-plugin to generate modules that are requireModuleAsync-loadable.

Syntax

requireModuleAsync<T>(path: string, callback: (error: Error | null, exports?: T) => void): void;

Parameters

path

The path of the module. Can be a remote path, or a path inside the template.

callback

The callback to be executed after the require is finished

  • If any error occurs, an error will be passed as the first parameter.
  • Otherwise, a null will be passed as the first parameter, and the exported value of the module will be passed as the second parameter.

Return Value

None (undefined).

Exception

requireModuleAsync will return an error in callback when:

  • Network error
  • Module execute error

Examples

Loading remote resources

lynx.requireModuleAsync('https://example.com/path/to/chunk', (err, exports) => {
  if (err) {
    return;
  }
  // use exports
});

Promisify

const requireModulePromise = (path) =>
  new Promise((resolve, reject) => {
    lynx.requireModuleAsync(path, (err, exports) => {
      if (err) {
        return reject(err);
      }
      return resolve(exports);
    });
  });

await requireModulePromise('path/to/chunk');

Compatibility

LCD tables only load in the browser

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.