"use strict"; const Promise = require('bluebird') class MyAction { constructor (name, nextAction, preFail, postFail) { this.name = name this.nextAction = nextAction this.preFail = preFail this.postFail = postFail } execute (context) { return Promise.bind(this) .then(function () { console.log(this.name+' pre') if (this.preFail) { throw new Error(this.name+' pre') } }) .then(function () { console.log(this.name+' mid') if (this.nextAction) { return this.nextAction.execute(context) } }) .then(function () { console.log(this.name+' post') if (this.postFail) { throw new Error(this.name+' post') } }) .catch(function (e) { console.log(this.name+' error:', e) throw e }) } // execute } // class let a3 = new MyAction('treci', null, true, false); let a2 = new MyAction('drugi', a3, false, false); let a1 = new MyAction('prvi', a2, false, false); a1.execute({}) .catch(function (e) { console.log('TOP error', e, e.stack) })