Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created July 21, 2013 17:25
Show Gist options
  • Save PatrickJS/6049229 to your computer and use it in GitHub Desktop.
Save PatrickJS/6049229 to your computer and use it in GitHub Desktop.
simple version of promises without then, when, chaining
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