th
Version:
th.en - make it painless to adapt between callbacks and promises
69 lines (41 loc) • 1.34 kB
Markdown
# then
make it painless to adapt between callbacks and promises
## usage
`then` is like a magic portal to warp between Promised land and the firey depths of Callback hell.
```js
var then = require('th')
var fs = require('fs')
fs.readFile('./tmp', then())
then(function (file) {
console.log('file')
}, function (err) {
console.log('errors!', err)
})
// although you may want to be using streaming file methods, anyhow
```
When we invoke `then()` the first time, it creates a callback to give to `fs.readFile`. When we call it the next time, it's like a Promises/A+ `then` method.
Importantly, there are times when we want to go back from a chain of promises to a callback:
```js
function (err, nextCb) {
if (err) { return nextCb(err) }
var promise = myPromise()
.then(someStep)
.then(oneMoreThing)
then.callback(promise, nextCb)
}
```
## api
### `then : () => Promise | (resolved: Function, rejected: Function) => Promise`
See usage note above for behavior.
### `then.callback(Promise<T>, Callback<T>) => void`
Registers the `callback` as a handler for promise `resolve` and `reject` events.
## installation
$ npm install th
## running the tests
From package root:
$ npm install
$ npm test
## contributors
- jden <jason@denizac.org>
## license
MIT. (c) MMXIII AgileMD http://agilemd.com