I've got a process right now that stats and hashes (potentially large) files. Right now I'm doing it this way:
function gen_hash (fn, callback) {
var hash = crypto.createHash('sha256');
var fh = fs.createReadStream(fn);
fh.on('data', d => hash.update(d));
fh.on('end', () => {
var digest = hash.digest('hex');
});
}
function gen_stat (fn, callback) {
fs.stat(fn, function (err, stats) {
if (err) {
if (err.code == "ENOENT") {
file.exists = false;
}
}
}
}
Then I have some code that allows a callback when both of them complete. As you can imagine, it's fairly ... involved.
I think this is a good match for Promises, but I don't know how to use them.
I tried some stuff like:
const Promises = require('bluebird');
const fs = Promises.promisifyAll(require('fs'));
fh = fs.createReadStream(fn)
.then...
.error...
But I don't actually understand what to do here, and the Bluebird website seems to lack details.
I feel like something like Promise.all
is probably the right thing, but I just don't see any good examples of the right syntax.
I understand that I can write a wrapper function that returns a promise on various conditions, but I don't quite get how that works in promise-land. Maybe something like this?
const fs = Promise.promisifyAll(require('fs'));
function promise_checksum (fn) {
return new Promise((fulfill, reject) => {
// What goes here...?
}
}
function promise_stat (fn) {
return new Promise((fulfill, reject) => {
fs.stat(fn).then((err, stats) => {
if (err) {
reject(err);
} else {
fulfill(stats);
}
}
}
}
function checksum_and_stat (fn) {
return new PromiseAll((fulfill, reject) => {
// What goes here?
});
}
Help?
You can wrap your function in a promise like this:
Sample Usage:
For regular functions in the node.js
fs
module, you can use Bluebird to promisify them.This will create new methods on the
fs
object that end with Async (in addition to the regular ones that are part of the standardfs
module) and, instead of accepting a callback, those new methods will return a promise. Bluebird will have shimmed each of these methods and provided it's own callback that manages the promise for you. Here's an example:Promise.all()
is used when you have multiple separate promises and you want to know when they are all fulfilled. It accepts an array of promises as it's argument and it returns a new master promise that will resolve with an array of values or rejects with the first error that occurred. So, you could do this: