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

Commit 9ba08deb by Jan Aagaard Meier

Implement .spread in custom event emitter

1 parent 9ae31380
......@@ -147,6 +147,19 @@ module.exports = (function() {
}).then(onFulfilled, onRejected)
}
CustomEventEmitter.prototype.spread = function(onFulfilled, onRejected) {
var self = this
onFulfilled = bindToProcess(onFulfilled)
onRejected = bindToProcess(onRejected)
return new Promise(function (resolve, reject) {
self.on('error', reject)
.on('success', function () {
resolve(Array.prototype.slice.apply(arguments)) // Transform args to an array
});
}).spread(onFulfilled, onRejected)
}
return CustomEventEmitter;
})()
......
......@@ -309,4 +309,52 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
})
})
})
describe('findOrCreate', function () {
beforeEach(function(done) {
this.User.create({ id: 1, aNumber: 0, bNumber: 0 }).done(done)
})
it('with then', function (done) {
this.User
.findOrCreate({ id: 1})
.then(function(user) {
expect(user.id).to.equal(1)
expect(arguments.length).to.equal(1)
done();
})
})
describe('with spread', function () {
it('user not created', function (done) {
this.User
.findOrCreate({ id: 1})
.spread(function(user, created) {
expect(user.id).to.equal(1)
expect(created).to.equal(false)
expect(arguments.length).to.equal(2)
done();
})
})
it('user created', function (done) {
this.User
.findOrCreate({ id: 2})
.spread(function(user, created) {
expect(user.id).to.equal(2)
expect(created).to.equal(true)
expect(arguments.length).to.equal(2)
done();
})
})
it('works for functions with only one return value', function (done) {
this.User
.find({ id: 1})
.spread(function(user) {
expect(user.id).to.equal(1)
expect(arguments.length).to.equal(1)
done();
})
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!