diff --git a/flatten-promise/README.md b/flatten-promise/README.md deleted file mode 100644 index f7f1459..0000000 --- a/flatten-promise/README.md +++ /dev/null @@ -1,16 +0,0 @@ -Here's the basic usage of the file that you'll be creating: - -```js -var flattenPromise = require('./') // <- this is the file you make; - -var p1 = new Promise(function(resovle) { resovle('done'); }); -var p2 = new Promise(function(resovle) { resovle(p1); }); -var p3 = new Promise(function(resovle) { resovle(p2); }); -var p4 = new Promise(function(resovle) { resovle(p3); }); - -flattenPromise(p4).then(function(result) { - console.log(result); // 'done' -}); -``` - -More info: http://www.html5rocks.com/en/tutorials/es6/promises/ diff --git a/flatten-promise/test.js b/flatten-promise/test.js deleted file mode 100644 index 96961b3..0000000 --- a/flatten-promise/test.js +++ /dev/null @@ -1,19 +0,0 @@ -var assert = require('assert'); -var flattenPromise = require('./'); - -describe('flattenPromise', function() { - - it('flattens the promises', function(done) { - var p1 = new Promise(function(resovle) { resovle('done'); }); - var p2 = new Promise(function(resovle) { resovle(p1); }); - var p3 = new Promise(function(resovle) { resovle(p2); }); - var p4 = new Promise(function(resovle) { resovle(p3); }); - - flattenPromise(p4).then(function(result) { - assert.equal(result, 'done'); - done(); - }); - }); - - -}); diff --git a/flatten-thunk/README.md b/flatten-thunk/README.md new file mode 100644 index 0000000..8bc31f4 --- /dev/null +++ b/flatten-thunk/README.md @@ -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 diff --git a/flatten-promise/package.json b/flatten-thunk/package.json similarity index 100% rename from flatten-promise/package.json rename to flatten-thunk/package.json diff --git a/flatten-thunk/test.js b/flatten-thunk/test.js new file mode 100644 index 0000000..df7d1c5 --- /dev/null +++ b/flatten-thunk/test.js @@ -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(); + }); + }); + + +});