require.async

0.1.1 • Public • Published

require.async

Build Status

Background

NodeJS has a synchronous require() because its module loading system is synchronous.

This works because NodeJS runs on the server which means IO calls to fetch module code will be fast (< 10 ms).

But not all module loading systems are synchronous.

Almost all module loading systems designed for the browser are asynchronous for the express purpose of working around higher IO latencies and keeping runtime sizes smaller. They do this by allowing for incremental loading of resources and in our case code bundles.

Code bundles are sets of statically linked modules that (exports = require("<id>")) each other.

One bundle can require another bundle using require.async(id, function success(exports), function error(err)). Notice how we are making an asynchronous call to fetch the next segment of the program we need to execute.

This approach of composing a program into bundles where one root bundle loads other bundles (which may load further bundles) is considered best practice and achieved in different ways by various toolchains.

All toolchains and runtimes can benefit from require.async() used by a developer (instead of require()) to indicate entry into an OPTIONAL / CONDITIONAL / RARE segment of the program. Programs written in this fashion are more portable.

The require.async method is inspired by CommonJS and supported by the PINF JavaScript Loader which can load code bundles conflict free into any modern JavaScript runtime.

What

require.async is a module that patches the require given to your module by the NodeJS module system.

It adds the method require.async(id, function success(exports), function error(err)) which you can use instead of require("<id>") to indicate entry into an OPTIONAL / CONDITIONAL / RARE segment of the program.

All plugin loaders for example should be using require.async to asynchronously load plugins.

Install

npm install require.async

Usage

package.json

{
    "name": "test"
}

main.js

require("require.async")(require);

require.async("./package.json", function(info) {
    console.log(JSON.stringify(json));
})l

Run:

$ node main.js
{"name": "test"}

License

Author: Christoph Dorn License: UNLICENSE

Readme

Keywords

none

Package Sidebar

Install

npm i require.async

Weekly Downloads

4

Version

0.1.1

License

none

Last publish

Collaborators

  • cadorn