Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
raksoras committed Dec 5, 2016
1 parent 93df553 commit c889a6d
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/raksoras/koroutine.svg?branch=master)](https://travis-ci.org/raksoras/koroutine)
[![Coverage Status](https://coveralls.io/repos/github/raksoras/koroutine/badge.svg?branch=master)](https://coveralls.io/github/raksoras/koroutine?branch=master)

Small, lightweight coroutine scheduler for node.js, based on ES6 generators
Small, lightweight non-blocking coroutine scheduler for node.js based on ES6 generators

##Table of Contents

Expand All @@ -22,26 +22,24 @@ $ npm install koroutine

## Introduction

This is a 100% javascript implementation of coroutine scheduler based on ES6 generators. It can be used
This is a 100% javascript implementation of a coroutine scheduler that uses ES6 generators. It can be used
to replace async callback spaghetti code with simpler, sequential looking code that is still 100% async.
You can either make async calls one after other or fire bunch of async calls in parallel and wait for all
of them to complete before retrieving results/errors for each of the calls.

## Sequential async calls example
## koroutine.run(generatorFn, timeout, ...rest)
Runs supplied generator function as a coroutine.

Use `koroutine.run(generator_funtion, timeout, argument_1, argument_2, ...)` to run any ES6 generator function
as a coroutine. It runs your generator function with `this` bound to the running coroutine context passing in all the
arguments passed to run() after the second parameter `timeout` as function arguments. You can then pass `this.resume` as a
callback to any async function you may want to call from inside of the `generator_function`. `resume` follows Node's callback
convention - i.e. first parameter is error followed by results or data parameters. If the async function returns an error, it
is thrown as an exception inside the generator function body as shown below.
* __this__ is bound to the running coroutine's context object (see "Coroutine Context Methods" below) inside the generator function.
* __timeout__ is maximum number of milliseconds coroutine is allowed to run. If it runs for more than that exception is thrown inside generator function with e.cause="timedout".
* __...rest__ are rest of the arguments that are passed in to generator function as its function arguments.

The second parameter `timeout` is the maximum amount of time in milliseconds that the coroutine is allowed to run. If it
runs for more than `timeout` milliseconds, an exception with e.cause="timedout" is thrown inside the generator function.
timeout=0 means no maximum time limit (infinite timeout).
### Sequential async calls example

Inside generator function `this` is bound to the running coroutine's context. You can pass `this.resume` as a
callback to any async function you may want to call from inside the generator function. `resume` follows Node's callback
convention - i.e. first parameter is error followed by results or data parameters. If the async function returns an error, it
is thrown as an exception inside the generator function body as shown below.
```js
const ko = require('koroutine');
const koroutine = require('koroutine');

function dummyAsyncSuccessCall(input, callback, delay) {
setTimeout(function() {
Expand All @@ -67,17 +65,17 @@ function* exampleKoroutine(input1, input2) {
}
}

ko.run(exampleKoroutine, 0, "myinput1", "myinput2");
koroutine.run(exampleKoroutine, 0, "myinput1", "myinput2");
```
### koroutine.join(...futures)
Waits till all the async operations represented by the futures passed are complete, in a non-blocking manner. On completion each future either has its `future.data` set to the result of the call (in case of success) or its `future.error` set to the error returned by the call.

## Parallel async calls example
### Parallel async calls example

You can also fire multiple async calls in parallel using Koroutine and then wait for all of them to complete. To do this, get
You fire multiple async calls in parallel using Koroutine and then wait for all of them to complete. To do this, get
`future` function objects by calling `this.future()` for each of the async calls and pass them as callbacks to the async calls
in place of `this.resume`. You can then wait for all of them to complete by calling `yield* koroutine.join(future1, future2,
...)`. It will return only when all the calls are complete - either succesfully or by returning error. Upon return, each
future object will either have its `data` member set to result - in case of a succesfull call - or its `error` member set to
the error returned by the corresponding call.
...)`.

```js
const ko = require('koroutine');
Expand All @@ -97,19 +95,9 @@ function* exampleKoroutine(input1, input2) {
console.log(future2.error);
}
```
## koroutine Library Object Methods

### run(generator, timeout, ...rest)
Runs generator function as a coroutine.

* __this__ is bound to the running coroutine's context object (see "Coroutine Context Methods" below) inside generator function.
* __timeout__ is maximum number of milliseconds coroutine is allowed to run. If it runs for more than that exception is thrown inside generator function with e.cause="timedout".
* __...rest__ are rest of the arguments that are passed in to generator function as its function arguments.

### *join(...futures)
Non-blocking wait till all the async operations represented by the futures passed are complete. On completion each future either has its `future.data` set to the result of the call (in case of success) or its `future.error` set to the error returned by the call.

### koroutine.current.context
## koroutine.current.context
Current running coroutine's variable storage. Similar to thread local variable in Java or pthread. All the typical issues with usage of thread local variables apply so use it with caution!

koroutine library will swap the appropriate `current.context` automatically when it swicthes between coroutines. A coroutine can store its local copy of any variable in the context like this
Expand All @@ -122,6 +110,7 @@ function* coroutineFn() {
}
```
variables stored in `koroutine.current.context` are local to the running coroutine and are not shared between two coroutines even if they share the same name.

## Coroutine Context Methods

### this.resume
Expand Down

0 comments on commit c889a6d

Please sign in to comment.