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 0ca7523 commit 61d56ba
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@

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

#Table of Contents
##Table of Contents

- [Install](#install)
- [Introduction](#introduction)
- [Koroutine Library Object Methods](#koroutine-library-object-methods)
- [Coroutine Context Methods](#coroutine-context-methods)

# Install
## Install

```sh
$ npm install koroutine
```

# Introduction
## Introduction

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.

# Koroutine Library Object Methods
## Koroutine Library Object Methods

###koroutine.run(generatorFn, timeout, ...rest)

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

* __this__ is bound to the running coroutine's context object (see [Coroutine Context Methods](#coroutine-context-methods) below) inside the generator function.
* __timeout__ is maximum number of milliseconds the coroutine is allowed to run. If it runs beyond that limit, an exception is thrown inside the generator function with e.cause="timedout".
* __...rest__ are rest of the arguments that are passed to generator function as its function arguments.

### Sequential async calls example
#### 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
Expand Down Expand Up @@ -68,10 +69,10 @@ function* exampleKoroutine(input1, input2) {
koroutine.run(exampleKoroutine, 0, "myinput1", "myinput2");
```

## koroutine.join(...futures)
### koroutine.join(...futures)
Non-blocking wait till all the async operations represented by the supplied futuresare complete. `future`s are obtained by calling `this.future()` from inside the generator function. 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.join()` returns total number of errors encountred.

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

You can fire multiple async calls in parallel and then wait for all of them to complete at a single point in your code as shown below.

Expand All @@ -96,7 +97,7 @@ function* exampleKoroutine(input1, input2) {
koroutine.run(exampleKoroutine, 0, "myinput1", "myinput2");
```

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

koroutine library will switch 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 @@ -111,20 +112,20 @@ 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
## Coroutine Context Methods

## this.resume
### this.resume
Callback you can pass to any async calls you want to make from inside the generator function. `this` is bound to current coroutine inside the generator function. Resume follows Node js callback convention where first parameter is an error followed by one or more result parameters.

## this.future()
### this.future()
Returns a `future` function object that can be used as a callback in place of `this.resume` when you want to make multiple async calls in paralell. See [Parallel Async Calls Example](#parallel-async-calls-example) above.

## this.sleep(ms)
### this.sleep(ms)
Non-blocking sleep for `ms` number of milliseconds.

## this.defer()
### this.defer()
Gives up CPU voluntarily. The coroutine will be resumed on the next event loop turn. Similar to `setImmediate()` or Thread.yield() in pthread library.

## this.cancel()
### this.cancel()
Allows cancelling the coroutine from outside the running coroutine. Causes an exception to be thrown inside the canceled coroutine with e.cause="canceled"

0 comments on commit 61d56ba

Please sign in to comment.