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

Commit e41a2a67 by Mick Hansen

feat(promises): have hooks.runHooks() return a promise

1 parent 6e754979
...@@ -1224,7 +1224,6 @@ module.exports = (function() { ...@@ -1224,7 +1224,6 @@ module.exports = (function() {
if (!!err) { if (!!err) {
return emitter.emit('error', err) return emitter.emit('error', err)
} }
daos = newRecords || daos daos = newRecords || daos
options.fields = newFields || options.fields options.fields = newFields || options.fields
......
var Utils = require("./utils") var Utils = require("./utils")
, Promise = require("./promise")
/** /**
* Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways: * Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways:
...@@ -69,13 +70,14 @@ Hooks.runHooks = function() { ...@@ -69,13 +70,14 @@ Hooks.runHooks = function() {
hooks = hooks === undefined ? [] : [hooks] hooks = hooks === undefined ? [] : [hooks]
} }
return new Promise(function (resolve, reject) {
if (hooks.length < 1) { if (hooks.length < 1) {
return fn.apply(this, [null].concat(args)) return resolve(args)
} }
var run = function(hook) { var run = function(hook) {
if (!hook) { if (!hook) {
return fn.apply(this, [null].concat(args)) return resolve(args)
} }
if (typeof hook === "object") { if (typeof hook === "object") {
...@@ -86,7 +88,7 @@ Hooks.runHooks = function() { ...@@ -86,7 +88,7 @@ Hooks.runHooks = function() {
tick++ tick++
if (!!arguments[0]) { if (!!arguments[0]) {
return fn(arguments[0]) return reject(arguments[0])
} }
// daoValues = newValues // daoValues = newValues
...@@ -95,6 +97,9 @@ Hooks.runHooks = function() { ...@@ -95,6 +97,9 @@ Hooks.runHooks = function() {
} }
run(hooks[tick]) run(hooks[tick])
}).spread(function () {
fn.apply(self, [null].concat(Array.prototype.slice.apply(arguments)));
}, fn);
} }
Hooks.hook = function() { Hooks.hook = function() {
......
...@@ -173,4 +173,10 @@ SequelizePromise.prototype.proxy = function(promise, options) { ...@@ -173,4 +173,10 @@ SequelizePromise.prototype.proxy = function(promise, options) {
return this return this
} }
SequelizePromise.prototype.proxySql = function(promise) {
return this.proxy(promise, {
events: ['sql']
});
};
module.exports = SequelizePromise; module.exports = SequelizePromise;
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!