简体   繁体   English

将承诺链分解为单独的功能

[英]Break promise chain into seperate functions

break into two functions:分为两个功能:

public valJson(json, schemaFile: string) {
    return new Promise((resolve, reject) => {
        this.http.get(schemaFile)
            .toPromise()
            .then(fileContents => fileContents.json())
            .then((schema) => {
                let ajv = new Ajv({allErrors: true});
                ajv.validate(schema, json) ? 
                resolve() :
                reject(new Error("JSON does not conform to schema: " + ajv.errorsText()));
            }, err => reject(
                new Error("Unable to get schema file contents:" + err))
            );
    });
};

There is really 3 things this function does so it isn't very good to unit test.这个函数确实做了三件事,所以单元测试不是很好。

  1. get file contents获取文件内容
  2. parse them to json将它们解析为 json
  3. validate the json验证json

How can this be broken up to make each of those tasks unit testable?如何将其分解以使每个任务都可进行单元测试?

Attempt 1 at breaking the actual validation part out is not going well:尝试 1 打破实际的验证部分并不顺利:

public valJson(json, schemaFile: string) {
    return new Promise((resolve, reject) => {
        this.http.get(schemaFile)
            .toPromise()
            .then(fileContents => fileContents.json())
            .then((schema) => {
                this.valJ(schema)
            }, err => reject(
                new Error("Unable to get schema file contents:" + err))
            );
    });
};

    valJ(schema, json) {
        let ajv = new Ajv({ allErrors: true });
        if ajv.validate(schema, json) 
            return resolve()
        reject(new Error("JSON does not conform to schema: " + ajv.errorsText()));
    }
}

update - As per the feedback I'm trying to stop using anonymous functions and then I will try to not create a new promise.更新 - 根据反馈,我试图停止使用匿名函数,然后我将尝试不创建新的承诺。 I'm here:我在这:

public valJson(json, schemaFile: string) {
    return new Promise((resolve, reject) => {
    var getFilePromise = this.http.get(schemaFile)
            .toPromise();
    var parseToJsonPromise = getFilePromise
            .then(contents => this.toJson(contents));
    var validateJsonPromise = parseToJsonPromise.then(schema => this.valJ(schema, json, resolve, reject),
            err => reject(
                new Error("Unable to get schema file contents:" + err))
            );
    });
};

toJson(fileContents): any {
    return fileContents.json()
}

valJ(schema, json, resolve, reject): any {
    let ajv = new Ajv({ allErrors: true });
    ajv.validate(schema, json) ?
        resolve() :
        reject(new Error("JSON does not conform to schema: " + ajv.errorsText())
    );
}

One possible approach:一种可能的方法:

public valJson(json, schemaFile: string) {
  return this.fetchSchema(schemaFile)
    .then(res => this.parseSchema(res))
    .then(schema => this.validateWithSchema(json, schema));
}

fetchSchema(url) {
  return this.http.get(url).toPromise();
}

parseSchema(response) {
  return response.json();
}

validateWithSchema(json, schema) {
  let ajv = new Ajv({allErrors: true});
  if (!ajv.validate(schema, json)) {
    throw new Error("JSON does not conform to schema: " + ajv.errorsText())
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM