all returning promises When your interfaces are all returning promises var Promise = require('bluebird'); /* ... */ ClipEntity.prototype.showVideos = Promise.method(function() { return this.fetchVideos(); });
Removing Clip from DB, clipId:', this.clipId); return this.clipEnt.delete(this.clipId) .bind(this) .catch(function (error) { log.warn('rollback() :: NOT GOOD ERROR. File:', this.sourceFilePath, ' Failed to remove record for clip with ID:', this.clipId, 'Error: throw error; }) .then(function() { throw err; }); }); The Rollback <--- From clipEnt.delete() <--- From Parent
var clipProcessEnt = new ClipProcessEnt(data); return clipProcessEnt.parseFiles() .bind(clipProcessEnt) .then(clipProcessEnt.isFileVideo) .then(clipProcessEnt.storeClip) .then(clipProcessEnt.storeThumb) .then(clipProcessEnt.updateDatabase) .then(clipProcessEnt.removeSourceFiles) .return(data) .catch(clipProcessEnt.rollback); }; <-- Here it is!
var clipProcessEnt = new ClipProcessEnt(data); var promises = []; promises.push(clipProcessEnt.isFileVideo()); promises.push(clipProcessEnt.storeClip()); promises.push(clipProcessEnt.storeThumb()); promises.push(clipProcessEnt.updateDatabase()); return Promise.all(promises, {concurrency: 10}) .catch(clipProcessEnt.rollback); }; ... with a throttle
of a module var fs = Promise.promisifyAll(require('fs')); fs.readFileAsync('one').then().catch(); // Promisify a single method var readFileAsync = Promise.promisify(fs.readFile); readFileAsync.then().catch();
.bind(this) .map(this.copyFile) .then(function(filenames) { return Promise.all([ this.process(filenames), this.save(filenames), this.notify(filenames), ]); }) .spread(function(fromProcess, fromSave, fromNotify) { return this.massup(fromProcess, fromSave, fromNotify) }) .then(this.compress) .then(this.sign) .then(this.publish) .catch(this.onError); }); <-- filenames is the result of copyfiles() <-- Parallel Async OPs <-- spread() is one to one with the array