Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 34 additions & 52 deletions source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,36 @@ inspired by Node.js modules.<br>Here's how it works.

# Creating a Module

We're going to build a simple `asap` module, which lets you schedule some
work to happen as soon as possible, but asynchronously. In Node.js, you
can do this via `process.nextTick`, but there are different approaches
that work in various browsers. We're going to build a module that works
everywhere.[<sup>1</sup>](#footnote-1)
We're going to build a simple `logger` module, which lets you log
information to the console.

We start by creating a new file for this module. We'll call it `asap.js`.
We start by creating a new file for this module. We'll call it `logger.js`.
The module provides a single primary function, which JavaScript calls
the *default export*. You export a default value from a module by using
`export default`.

```js
var asap;
var isNode = typeof process !== "undefined" &&
{}.toString.call(process) === "[object process]";

if (isNode) {
asap = process.nextTick;
} else if (typeof setImmediate !== "undefined") {
asap = setImmediate;
} else {
asap = setTimeout;
}
var logThis;

logThis = function(thingToLog) {
console.log(thingToLog);
};

export default asap;
export default logThis;
```

# Importing a module

To import `asap` in another module, we use the import syntax:
To import `logger` in another module, we use the import syntax:

```js
import asap from "asap";
import logMe from "logger";

asap(function() {
console.log("hello async world!");
});
logMe("hello world");
```

This syntax takes the default function exported from `asap` and stores it
in the variable `asap`, which we can then use to call it.
This syntax takes the default function exported from `logger` and stores it
in the variable `logMe`, which we can then use to call it.

# Named exports

Expand All @@ -60,47 +49,45 @@ function), and several additional named exports (`ajax`, `getJSON`,
export, which recursively creates a directory, and a named export called
`sync`, which does the same thing, but synchronously.

In our case, in addition to the default export, the `asap` module might
wish to provide a `later` function, which schedules a function to run
later, after other network or UI work has a chance to occur.
In our case, in addition to the default export, the `logger` module might
wish to provide a `logLater` function, which logs something after a
specified amount of time.

Our module looks the same as before, except we add a new export
declaration.

```js
var asap;
var isNode = typeof process !== "undefined" &&
{}.toString.call(process) === "[object process]";

if (isNode) {
asap = process.nextTick;
} else if (typeof setImmediate !== "undefined") {
asap = setImmediate;
} else {
asap = setTimeout;
}
var logThis;

logThis = function(thingToLog) {
console.log(thingToLog);
};

export default asap;
export var later = isNode ? process.setImmediate : asap;
logThisLater = function(thingToLog, delay) {
window.setTimeout(function() {
console.log(thingToLog);
}, delay);
};

export default logThis;
export var logLater = logThisLater;
```

# Named imports

Now that we've exported `later`, we can import it in another module.
Now that we've exported `logLater`, we can import it in another module.

```js
import { later } from "asap";
import { logLater } from "logger";

later(function() {
console.log("Running after other network events");
});
logLater("See you in 5 seconds", 5000);
```

For the curious, you can import both the default export and a number of
named exports in the same `import`:

```js
import asap, { later } from "asap";
import logMe, { logLater } from "logger";
```

And that's all there is to it!
Expand Down Expand Up @@ -195,8 +182,3 @@ use cases smooth and seamless, including refactoring and tooling.
importing by just looking at the syntax. That improves error
messages, but also makes it easier to build tools like browserify and
JSHint that work reliably without caveats.

# Notes

<span id="footnote-1"><sup>1</sup> A production-quality version of this library would be more
detailed, but this will do for our purposes.</span>