Created
July 21, 2013 17:25
-
-
Save PatrickJS/6049229 to your computer and use it in GitHub Desktop.
simple version of promises without then, when, chaining
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
var fs = require('fs'); | |
module.export.fileObj = function(file, encode) { | |
var status = 'pending', | |
doneStack = [], | |
failStack = [], | |
done, fail, fullFail, fullData, | |
rect = {}; | |
fs.fileRead(file, encode, function(data, err) { | |
if (!err) { | |
doneStack.forEach(function(doneCallback) { | |
doneCallback(data); | |
fullData = data; | |
status = 'success'; | |
}); | |
} else { | |
failStack.forEach(function(failCallback) { | |
failCallback(err); | |
fullFail = err; | |
status = 'fail'; | |
}); | |
} | |
}); | |
done = function(callback) { | |
if (status === 'success') { | |
callbaack(fullData); | |
} else { | |
doneStack.push(callback); | |
} | |
return rect; | |
}; | |
fail = function(callback) { | |
if (status === 'fail') { | |
fullFail(fullFail); | |
} else { | |
failStack.push(callback); | |
} | |
}; | |
rect.done = done; | |
rect.fail = fail; | |
return rect; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment