Skip to content

Commit

Permalink
Fix some grammar mistakes in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
BYVoid committed Dec 8, 2012
1 parent b88c92f commit 13484d2
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Continuation.js

Continuation.js is a compiler for asynchronous [Continuation-Passing Style](http://en.wikipedia.org/wiki/Continuation-passing_style) transformation, which simplifies asynchronous JavaScript programming. It can be also called a translator, because it translates slightly flavored JavaScript syntax into standard JavaScript. Continuation.js introduces a virtual function ``cont``, which allow you to write continuation-passing style code (or asynchronous callback style code) far easier. `cont` is not a actual function, but a mark with the same syntax to function in JavaScript. By using it you can write asynchronous control flows like flat threaded code, and Continuation.js compiles it into continuation-passing style code.
Continuation.js is a compiler for [Continuation-Passing Style](http://en.wikipedia.org/wiki/Continuation-passing_style) transformation, which simplifies asynchronous JavaScript programming.
It translates slightly flavored JavaScript syntax into standard JavaScript, so it can be also called a "translator".
Continuation.js introduces a virtual function ``cont``, which allow you to write continuation-passing style code (or asynchronous callback style code) far easier.
`cont` is not a actual function, but a mark with the same syntax to function calls in JavaScript.
By using Continuation.js you can write asynchronous control flows like flat threaded code, and it compiles it into continuation-passing style code.

Typically, writing with asynchronous control flow is a pain, because you will easily write nested callbacks like below:

Expand All @@ -26,7 +30,8 @@ textProcessing(function (err, contents) {
});
```

This kind of coding style is called 'callback hells' or 'callback pyramids'. While using Continuation.js, you directly write:
This kind of coding style is called "callback hells" or "callback pyramids".
While using Continuation.js, you directly write:

```javascript
function textProcessing(ret) {
Expand All @@ -45,9 +50,16 @@ if (err)
console.error(err);
```

The code above is flatted by using virtual function call ``cont``. The control flow must "wait" for return of the asynchronous function call ``fs.readFile``. And parameters in the argument list of ``cont`` will be set after it returns. "Return" here is a little confusing because in an asynchronous function "return" means callback function called, not "return" in the literal sense. Because an asynchronous function usually returns immediately (by encountering ``return`` statement or the end of the function scope) while the callback function could be called later. You can be understood as all lines after ``cont`` until the end of the function are the callback function of the asynchronous function call. The code feels like threaded code, but it is still asynchronous while executing.
The code above is flatted by using the virtual function ``cont``.
Control flow must "wait" for the return of asynchronous function call ``fs.readFile``.
Parameters in the argument list of ``cont`` will be set after it returns.
"Return" here is a little confusing because in an asynchronous function "return" means callback function called, not "return" in the literal sense.
An asynchronous function usually returns immediately (by encountering ``return`` statement or the end of the function scope) while the callback function could be called later.
You can be understood as all the statements after ``cont`` until the end of the function are the callback function of the asynchronous function call.
The code feels like threaded code, but it is still asynchronous while executing.

Even more simply, the asynchronous error can be held with JavaScript ``try...catch`` syntax and another virtual function call ``obtain`` provided by Continuation.js. ``obtain(a)`` is equivalent to ``cont(err, a)``, and ``err`` would be thrown if it is not ``undefined``.
Even more simply, the asynchronous error can be held with JavaScript ``try...catch`` syntax and another virtual function call ``obtain`` provided by Continuation.js.
``obtain(a)`` is equivalent to ``cont(err, a)``, and ``err`` would be thrown if it is not ``undefined``.

```javascript
function textProcessing(ret) {
Expand Down Expand Up @@ -83,14 +95,17 @@ try {

### cont

``cont`` is a mark of asynchronous calls. It must be used in the place of callback function parameter of a function call. The parameters of ``cont`` will be set to the values of parameters of the callback function. If parameter of ``cont`` is a variable (not an expression), it will be defined automatically before the function call.
``cont`` is a mark of asynchronous calls.
It must be used in the place of callback function parameter of a function call.
The parameters in ``cont`` will be set to the values of arguments of the callback function.
If parameter of ``cont`` is a variable (not an expression), it will be defined automatically before the function call.

Example:

```javascript
setTimeout(cont(), 1000);

fs.lstat('/path', cont(err, stats));
fs.lstat('/path/file', cont(err, stats));

var obj;
fs.readdir('/path', cont(err, obj.files));
Expand All @@ -114,7 +129,9 @@ setTimeout(function () {

### obtain

``obtain`` is a syntax sugar of ``cont`` and ``throw``. You can use ``try`` to catch asynchronous errors with ``obtain``. One assumption is that the error object it the first parameter of the callback function (this is a convention of Node.js API).
``obtain`` is a syntax sugar of ``cont`` and ``throw``.
You can use ``try`` to catch asynchronous errors with ``obtain``.
One assumption is that the error object is the first parameter of the callback function (this is a convention of Node.js API).

Example:

Expand Down Expand Up @@ -156,15 +173,16 @@ function f2() {

### parallel

``parallel`` allows you to execute asynchronous functions "in parallel". ``parallel`` is also a "virtual function" and its parameters should all be function calls with ``cont`` or ``obtain``.
All function calls in ``parallel`` will be executed in parallel and control flow continues executing the next statement after all the parallel function calls "return" (precisely speaking callback function called).
``parallel`` allows you to execute asynchronous functions "in parallel".
``parallel`` is also a virtual function and its parameters should be all function calls with ``cont`` or ``obtain``.
All function calls in ``parallel`` will be executed in parallel and control flow continues executing the next statement after all the parallel function calls "return" (precisely speaking callback functions called).

Note that both Node.js and browser execute JavaScript code in one thread, so this is not a multithreaded implementation.
They feel like threads so they can be called "lightweight threads".
Note that both Node.js and browser execute JavaScript code in single thread, so this is not a multithreaded implementation.
The function calls feel like entrances of threads so they can be called "lightweight threads".
Only I/O and computing are executed in parallel while computing are actually executed in sequence.
That is, when one "thread" is "blocked" by I/O, the other one runs.
The "threads" can automatically utilize the gaps of I/O and computing.
So you should let functions that both have I/O and computing run in parallel, not all functions with heavy computing and little I/O.
So you should let functions that both have I/O and computing run in parallel, rather than all functions with heavy computing and little I/O.

Example:

Expand All @@ -180,17 +198,23 @@ console.log(contents);

### Explicit mode

Modules can be written in Continuation.js and they will be recursively compiled automatically when using ``require``. Add ``'use continuation'`` into your source file, and use ``continuation script.js --explicit`` to run it and only files contains ``'use continuation'`` will be compiled. This option can reduce loading time if many modules are recursively required.
Modules can be written in Continuation.js and they will be recursively compiled automatically when using ``require``.
Add ``'use continuation'`` into your source file, and use ``continuation script.js --explicit`` to run it and only files contains ``'use continuation'`` will be compiled.
This option can reduce loading time if many modules are recursively required.

### Compilation cache

By using ``contination script.js --cache [cacheDir]``, all modules compiled by Contination.js will be cached into ``cacheDir``. If your source file is newer than the cached version, it will be compiled again. This rely on the system clock and timestamp. This option can also reduce loading time. It is recommended to use ``--explicit`` and ``--cache`` together.
By using ``contination script.js --cache [cacheDir]``, all modules compiled by Contination.js will be cached into ``cacheDir``.
If the timestamp of your source file is newer than the cached version, it will be compiled again.
This option rely on the system clock and it can also reduce loading time.
It is recommended to use ``--explicit`` and ``--cache`` together.

By default ``cacheDir`` is ``/tmp/continuation``.

### Use Continuation.js in CoffeeScript (and other compile-to-js language)

Continuation.js is compatible with most kinds of compile-to-js language because it introduces no non-primitive syntax. The only 3 keywords ``cont``, ``obtain`` and ``parallel`` are all virtual functions so you can use function call directly in your language.
Continuation.js is compatible with most kinds of compile-to-js language because it introduces no non-primitive syntax.
The only 3 keywords ``cont``, ``obtain`` and ``parallel`` are all virtual functions so you can use function call directly in your language.

Example (CoffeeScript):

Expand All @@ -206,7 +230,9 @@ Until now [CoffeeScript](http://coffeescript.org/) and [LiveScript](http://lives

### Use Continuation.js programmatically

You are able to use Continuation.js programmatically. Continuation.js module has one interface function ``compile(code)``. ``code`` must be a string.
You are able to use Continuation.js programmatically.
Continuation.js module has one interface function ``compile(code)``.
``code`` must be a string.

Example:

Expand All @@ -231,11 +257,14 @@ eval(compiledCode);
fibonacci();
```

You can run the code above using ``node`` command. That means you don't have to install Continuation.js in global. The code above converts a function into a string, and then it is compiled by Continuation.js. After that you can run it via ``eval`` function.
You can run the code above using ``node`` command.
That means you don't have to install Continuation.js in global environment.
The code above converts a function into a string, and then it is compiled by Continuation.js.
After that you can run it via ``eval`` function.

## Installation

Install Continuation.js with [NPM](https://npmjs.org/package/continuation):
Install Continuation.js with [npm](https://npmjs.org/package/continuation):

npm install -g continuation

Expand Down

0 comments on commit 13484d2

Please sign in to comment.