不要怂,就是干,撸起袖子干!

Commit 6e754979 by Mick Hansen

feat(promises): make sure chaining works

1 parent af7d2d7e
Showing with 30 additions and 8 deletions
...@@ -3,6 +3,7 @@ var util = require("util") ...@@ -3,6 +3,7 @@ var util = require("util")
, EventEmitter = require("events").EventEmitter , EventEmitter = require("events").EventEmitter
, proxyEventKeys = ['success', 'error', 'sql'] , proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils') , Utils = require('./utils')
, INTERNAL = function() {}
var SequelizePromise = function(resolver) { var SequelizePromise = function(resolver) {
var self = this; var self = this;
...@@ -28,7 +29,12 @@ var SequelizePromise = function(resolver) { ...@@ -28,7 +29,12 @@ var SequelizePromise = function(resolver) {
}.bind(this)); }.bind(this));
}; };
util.inherits(SequelizePromise, Promise); util.inherits(SequelizePromise, Promise)
// Need to hack then to make sure our promise is chainable
SequelizePromise.prototype.then = function (didFulfill, didReject, didProgress) {
return this._then(didFulfill, didReject, didProgress, void 0, new SequelizePromise(function () {}), this.then);
};
SequelizePromise.prototype.on = function(evt, fct) { SequelizePromise.prototype.on = function(evt, fct) {
if (evt === 'success') { if (evt === 'success') {
...@@ -72,6 +78,7 @@ SequelizePromise.prototype.emit = function(evt) { ...@@ -72,6 +78,7 @@ SequelizePromise.prototype.emit = function(evt) {
* @alias ok * @alias ok
* @return this * @return this
*/ */
SequelizePromise.prototype.success = SequelizePromise.prototype.success =
SequelizePromise.prototype.ok = function(fct) { SequelizePromise.prototype.ok = function(fct) {
this.then(fct); this.then(fct);
......
...@@ -421,6 +421,21 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -421,6 +421,21 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
}); });
}); });
it('should still work with .complete() after chaining', function(done) {
var spy = sinon.spy()
, promise = new SequelizePromise(function (resolve, reject) {
resolve('Heyo');
});
promise.then(function (result) {
return result+'123';
}).complete(function (err, result) {
expect(err).not.to.be.ok;
expect(result).to.equal('Heyo123');
done();
});
});
it('should still work with .success() when emitting', function(done) { it('should still work with .success() when emitting', function(done) {
var spy = sinon.spy() var spy = sinon.spy()
, promise = new SequelizePromise(function (resolve, reject) { , promise = new SequelizePromise(function (resolve, reject) {
...@@ -514,8 +529,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -514,8 +529,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
describe("proxy", function () { describe("proxy", function () {
it("should correctly work with success listeners", function(done) { it("should correctly work with success listeners", function(done) {
var emitter = new SequelizePromise() var emitter = new SequelizePromise(function () {})
, proxy = new SequelizePromise() , proxy = new SequelizePromise(function () {})
, success = sinon.spy() , success = sinon.spy()
emitter.success(success) emitter.success(success)
...@@ -531,8 +546,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -531,8 +546,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
}) })
it("should correctly work with error listeners", function(done) { it("should correctly work with error listeners", function(done) {
var emitter = new SequelizePromise() var emitter = new SequelizePromise(function () {})
, proxy = new SequelizePromise() , proxy = new SequelizePromise(function () {})
, error = sinon.spy() , error = sinon.spy()
emitter.error(error) emitter.error(error)
...@@ -549,8 +564,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -549,8 +564,8 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
}) })
it("should correctly work with complete/done listeners", function(done) { it("should correctly work with complete/done listeners", function(done) {
var promise = new SequelizePromise() var promise = new SequelizePromise(function () {})
, proxy = new SequelizePromise() , proxy = new SequelizePromise(function () {})
, complete = sinon.spy() , complete = sinon.spy()
promise.complete(complete) promise.complete(complete)
...@@ -569,7 +584,7 @@ describe(Support.getTestDialectTeaser("Promise"), function () { ...@@ -569,7 +584,7 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
describe("when emitting an error event with an array of errors", function() { describe("when emitting an error event with an array of errors", function() {
describe("if an error handler is given", function() { describe("if an error handler is given", function() {
it("should return the whole array", function(done) { it("should return the whole array", function(done) {
var emitter = new SequelizePromise() var emitter = new SequelizePromise(function () {})
var errors = [ var errors = [
[ [
new Error("First error"), new Error("First error"),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!