-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renamed flatten-promise to flatten-thunk
- Loading branch information
Moshe Kolodny
committed
Apr 22, 2015
1 parent
7c7e879
commit 285db6f
Showing
5 changed files
with
74 additions
and
35 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Here's the basic usage of the file that you'll be creating: | ||
|
||
```js | ||
var flattenThunk = require('./') // <- this is the file you make; | ||
|
||
var thunk1 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, 'done'); | ||
}, 1); | ||
} | ||
var thunk2 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, thunk1); | ||
}, 1); | ||
} | ||
var thunk3 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, thunk2); | ||
}, 1); | ||
} | ||
|
||
flattenThunk(thunk3)(function(err, result) { | ||
console.log(result); // 'done' | ||
}); | ||
``` | ||
|
||
A thunk is basically a function that you call with just the callback as a parameter: | ||
|
||
```js | ||
|
||
// this is a regular node CPS function | ||
fs.readFile('package.json', function(err, result) { | ||
console.log(result); | ||
}); | ||
|
||
// this is a thunk | ||
var readFileThunk = fs.readFileThunkily('package.json'); | ||
readFileThunk(function(err, result) { | ||
console.log(result); | ||
}); | ||
``` | ||
|
||
More info: https://github.com/tj/node-thunkify |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var assert = require('assert'); | ||
var flattenThunk = require('./'); | ||
|
||
describe('flattenThunk', function() { | ||
|
||
it('flattens the promises', function(done) { | ||
|
||
var thunk1 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, 'done'); | ||
}, 1); | ||
} | ||
var thunk2 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, thunk1); | ||
}, 1); | ||
} | ||
var thunk3 = function(cb) { | ||
setTimeout(function() { | ||
cb(null, thunk2); | ||
}, 1); | ||
} | ||
|
||
flattenThunk(thunk3)(function(err, result) { | ||
assert.equal(result, 'done'); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
}); |