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

Commit e0343afe by Andy Edwards Committed by GitHub

refactor: remove bluebird Promise.try and Promise.prototype.reflect usage (#12068)

1 parent 65600861
......@@ -293,7 +293,7 @@ class ConnectionManager {
* @returns {Promise}
*/
releaseConnection(connection) {
return Promise.try(() => {
return Promise.resolve().then(() => {
this.pool.release(connection);
debug('connection released');
});
......
......@@ -68,8 +68,8 @@ class InstanceValidator {
this.inProgress = true;
return Promise.all([
this._perAttributeValidators().reflect(),
this._customValidators().reflect()
this._perAttributeValidators(),
this._customValidators()
]).then(() => {
if (this.errors.length) {
throw new sequelizeError.ValidationError(null, this.errors);
......@@ -116,7 +116,7 @@ class InstanceValidator {
/**
* Will run all the validators defined per attribute (built-in validators and custom validators)
*
* @returns {Promise<Array.<Promise.PromiseInspection>>} A promise from .reflect().
* @returns {Promise<Array>}
* @private
*/
_perAttributeValidators() {
......@@ -140,7 +140,7 @@ class InstanceValidator {
}
if (Object.prototype.hasOwnProperty.call(this.modelInstance.validators, field)) {
validators.push(this._singleAttrValidate(value, field, rawAttribute.allowNull).reflect());
validators.push(this._singleAttrValidate(value, field, rawAttribute.allowNull));
}
});
......@@ -150,7 +150,7 @@ class InstanceValidator {
/**
* Will run all the custom validators defined in the model's options.
*
* @returns {Promise<Array.<Promise.PromiseInspection>>} A promise from .reflect().
* @returns {Promise<Array>}
* @private
*/
_customValidators() {
......@@ -162,8 +162,7 @@ class InstanceValidator {
const valprom = this._invokeCustomValidator(validator, validatorType)
// errors are handled in settling, stub this
.catch(() => {})
.reflect();
.catch(() => {});
validators.push(valprom);
});
......@@ -206,7 +205,7 @@ class InstanceValidator {
// Custom validators should always run, except if value is null and allowNull is false (see #9143)
if (typeof test === 'function') {
validators.push(this._invokeCustomValidator(test, validatorType, true, value, field).reflect());
validators.push(this._invokeCustomValidator(test, validatorType, true, value, field));
return;
}
......@@ -218,12 +217,14 @@ class InstanceValidator {
const validatorPromise = this._invokeBuiltinValidator(value, test, validatorType, field);
// errors are handled in settling, stub this
validatorPromise.catch(() => {});
validators.push(validatorPromise.reflect());
validators.push(validatorPromise);
});
return Promise
.all(validators)
.then(results => this._handleReflectedResult(field, value, results));
.all(validators.map(validator => validator.catch(rejection => {
const isBuiltIn = !!rejection.validatorName;
this._pushError(isBuiltIn, field, rejection, value, rejection.validatorName, rejection.validatorArgs);
})));
}
/**
......@@ -368,29 +369,6 @@ class InstanceValidator {
}
}
/**
* Handles the returned result of a Promise.reflect.
*
* If errors are found it populates this.error.
*
* @param {string} field The attribute name.
* @param {string|number} value The data value.
* @param {Array<Promise.PromiseInspection>} promiseInspections objects.
*
* @private
*/
_handleReflectedResult(field, value, promiseInspections) {
for (const promiseInspection of promiseInspections) {
if (promiseInspection.isRejected()) {
const rejection = promiseInspection.error();
const isBuiltIn = !!rejection.validatorName;
this._pushError(isBuiltIn, field, rejection, value, rejection.validatorName, rejection.validatorArgs);
}
}
}
/**
* Signs all errors retaining the original.
*
......
......@@ -1279,7 +1279,7 @@ class Model {
const attributes = this.tableAttributes;
const rawAttributes = this.fieldRawAttributesMap;
return Promise.try(() => {
return Promise.resolve().then(() => {
if (options.hooks) {
return this.runHooks('beforeSync', options);
}
......@@ -1707,7 +1707,7 @@ class Model {
? options.rejectOnEmpty
: this.options.rejectOnEmpty;
return Promise.try(() => {
return Promise.resolve().then(() => {
this._injectScope(options);
if (options.hooks) {
......@@ -2021,7 +2021,7 @@ class Model {
* @returns {Promise<number>}
*/
static count(options) {
return Promise.try(() => {
return Promise.resolve().then(() => {
options = Utils.cloneDeep(options);
options = _.defaults(options, { hooks: true });
options.raw = true;
......@@ -2459,7 +2459,7 @@ class Model {
options.fields = changed;
}
return Promise.try(() => {
return Promise.resolve().then(() => {
if (options.validate) {
return instance.validate(options);
}
......@@ -2487,7 +2487,7 @@ class Model {
delete updateValues[this.primaryKeyField];
}
return Promise.try(() => {
return Promise.resolve().then(() => {
if (options.hooks) {
return this.runHooks('beforeUpsert', values, options);
}
......@@ -2595,7 +2595,7 @@ class Model {
}
}
return Promise.try(() => {
return Promise.resolve().then(() => {
// Run before hook
if (options.hooks) {
return model.runHooks('beforeBulkCreate', instances, options);
......@@ -2916,7 +2916,7 @@ class Model {
let instances;
return Promise.try(() => {
return Promise.resolve().then(() => {
// Run before hook
if (options.hooks) {
return this.runHooks('beforeBulkDestroy', options);
......@@ -2989,7 +2989,7 @@ class Model {
Utils.mapOptionFieldNames(options, this);
return Promise.try(() => {
return Promise.resolve().then(() => {
// Run before hook
if (options.hooks) {
return this.runHooks('beforeBulkRestore', options);
......@@ -3092,7 +3092,7 @@ class Model {
let instances;
let valuesUse;
return Promise.try(() => {
return Promise.resolve().then(() => {
// Validate
if (options.validate) {
const build = this.build(values);
......@@ -3932,7 +3932,7 @@ class Model {
this.dataValues[createdAtAttr] = this.constructor._getDefaultTimestamp(createdAtAttr) || now;
}
return Promise.try(() => {
return Promise.resolve().then(() => {
// Validate
if (options.validate) {
return this.validate(options);
......@@ -4230,7 +4230,7 @@ class Model {
force: false
}, options);
return Promise.try(() => {
return Promise.resolve().then(() => {
// Run before hook
if (options.hooks) {
return this.constructor.runHooks('beforeDestroy', this, options);
......@@ -4299,7 +4299,7 @@ class Model {
force: false
}, options);
return Promise.try(() => {
return Promise.resolve().then(() => {
// Run before hook
if (options.hooks) {
return this.constructor.runHooks('beforeRestore', this, options);
......
......@@ -582,7 +582,7 @@ class Sequelize {
options.searchPath = 'DEFAULT';
}
return Promise.try(() => {
return Promise.resolve().then(() => {
if (typeof sql === 'object') {
if (sql.values !== undefined) {
if (options.replacements !== undefined) {
......@@ -633,7 +633,7 @@ class Sequelize {
const retryOptions = Object.assign({}, this.options.retry, options.retry || {});
return Promise.resolve(retry(() => Promise.try(() => {
return Promise.resolve(retry(() => Promise.resolve().then(() => {
if (options.transaction === undefined && Sequelize._cls) {
options.transaction = Sequelize._cls.get('transaction');
}
......@@ -797,7 +797,7 @@ class Sequelize {
}
}
return Promise.try(() => {
return Promise.resolve().then(() => {
if (options.hooks) {
return this.runHooks('beforeBulkSync', options);
}
......@@ -1114,7 +1114,7 @@ class Sequelize {
.catch(err => {
// Rollback transaction if not already finished (commit, rollback, etc)
// and reject with original error (ignore any error in rollback)
return Promise.try(() => {
return Promise.resolve().then(() => {
if (!transaction.finished) return transaction.rollback().catch(() => {});
}).throw(err);
});
......
......@@ -18,7 +18,7 @@ if (dialect.match(/^postgres/)) {
describe('createSchema', () => {
beforeEach(function() {
// make sure we don't have a pre-existing schema called testSchema.
return this.queryInterface.dropSchema('testschema').reflect();
return this.queryInterface.dropSchema('testschema').catch(() => {});
});
it('creates a schema', function() {
......@@ -64,9 +64,9 @@ if (dialect.match(/^postgres/)) {
// ensure the function names we'll use don't exist before we start.
// then setup our function to rename
return this.queryInterface.dropFunction('rftest1', [])
.reflect()
.catch(() => {})
.then(() => this.queryInterface.dropFunction('rftest2', []))
.reflect()
.catch(() => {})
.then(() => this.queryInterface.createFunction('rftest1', [], 'varchar', 'plpgsql', 'return \'testreturn\';', {}));
});
......@@ -87,14 +87,14 @@ if (dialect.match(/^postgres/)) {
// test suite causing a failure of afterEach's cleanup to be called.
return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }])
// suppress errors here. if create_job doesn't exist thats ok.
.reflect();
.catch(() => {});
});
after(function() {
// cleanup
return this.queryInterface.dropFunction('create_job', [{ type: 'varchar', name: 'test' }])
// suppress errors here. if create_job doesn't exist thats ok.
.reflect();
.catch(() => {});
});
it('creates a stored procedure', function() {
......@@ -211,7 +211,7 @@ if (dialect.match(/^postgres/)) {
// make sure we have a droptest function in place.
return this.queryInterface.createFunction('droptest', [{ type: 'varchar', name: 'test' }], 'varchar', 'plpgsql', body, options)
// suppress errors.. this could fail if the function is already there.. thats ok.
.reflect();
.catch(() => {});
});
it('can drop a function', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!