korubaku

Watch out for the Callback Hell!


Keywords
callback, hell, yield, async
License
GPL-3.0
Install
npm install korubaku@1.0.1

Documentation

Introduction

Korubaku (Katakana spelling for 'callback', コルバク) is a toy node.js module.

Korubaku intends to save you from the Callback Hell, using ES6's yield and generator.

A simple example

{korubaku, registerCallback} = require 'korubaku'

registerCallback 'dummy', ->
	'Just a test'

korubaku (ko) ->
	result = yield setTimeout ko.dummy(), 1000
	console.log result

execute the code above, it will wait for a second, and output 'Just a test'.

A function that yields is called a generator function. When a function yields, it suspends itself, and waits for calling next. If a value is passed when calling next, it will be the return value of yield when the generator function is resumed.

A generator function passed to korubaku function will be executed and a callback object ko will be passed as the first argument. The object has methods that generates node-style callback functions. On calling the generated callback, it should resume the generator function, pass back the final result to where it was yielded.

By default, ko has a method default, which generates the callback for most cases. It accepts two arguments, and the first one should be an Error. If the Error is not null, throw it. If null, resume the generator function and pass the second argument as a result. Callbacks can be extended with registerCallback name, function. The callback function should accept all callback parameters and return the final result or throw the error. Once registered, a new method named the same as the callback's name will be created in the object called ko above which can be used to generate node-style callbacks.

More examples

request
request = require "request"
{korubaku} = require "korubaku"

korubaku (ko) ->
	res = yield request.get 'https://google.com', ko.default()
	console.log "response #{res.statusCode}"
child_process
{exec} = require 'child_process'
{korubaku} = require 'korubaku'

korubaku (ko) ->
	stdout = yield exec 'echo "Tesuto sukuriputo"', ko.default()
	console.log stdout