虽然我有点弄清楚 Koa 流程机制是如何工作的(我认为),但我似乎无法掌握 co 和 co.wrap 之间的所有差异。这是给出意外行为的代码:
"use strict";
var co = require("co");
function ValidationError(message, obj, schema) {
Error.call(this, "Validation failed with message \"" + message + "\".");
this.name = "ValidationError";
this.object = obj;
this.schema = schema;
}
ValidationError.prototype = Object.create(Error.prototype);
function ValidatorWithSchema(properties, schema) {
this.properties = properties;
this.schema = schema;
}
ValidatorWithSchema.prototype.validate = function* (obj) {
var validatedObj = obj;
for (let schemaKey in this.schema) {
validatedObj = yield this.properties[schemaKey](validatedObj, this.schema[schemaKey]);
}
return validatedObj;
};
var typeGen = function* (obj, type) {
console.log("Checking against "+ type.name);
var primitives = new Map([
[String, "string"],
[Number, "number"],
[Boolean, "boolean"]
]);
if (!((obj instanceof type) || (primitives.has(type) && (typeof obj === primitives.get(type))))) {
var error = new ValidationError("Given object is not of type " + type.name, obj);
throw error;
}
return obj;
};
var validator = new ValidatorWithSchema({type: typeGen}, {type: String});
var runWrap = r => {
console.log(r);
console.log("### WRAP ###");
var validate = co.wrap(validator.validate);
validate(11).then(console.log, console.error);
};
co(function* () {
yield validator.validate(11);
}).then(runWrap, runWrap);
此代码的输出如下:
Checking against String
{ [ValidationError] name: 'ValidationError', object: 11, schema: undefined }
### WRAP ###
11
你可以看到我包装了 co.wrap 的使用,以便它在简单的 co 使用之后。现在很明显typeGen
在第二次尝试中没有被调用,但为什么会这样呢?这两个结果不应该是相同的吗?