2017-01-19 206 views
0

内我的代码块中的函数:异步函数异步函数

this.apiService.fetchCategories(!this.cacheData).subscribe(
    response => { 
     if(this._jsnValService.valCategories(response)) { 
      this.customerMap.categories = this.formatCategories(response["categories"]); 
     } else { 
      alert("Categories failed the schema validation. Please contact support if this happens again."); 
     } 
    }, 
    error => { 
     this.notification.title = "Oops, there's a problem."; 
     this.notification.content = "Seems there's an issue getting the provider categories."; 
     this.notification.show("provider_categories_api"); 
    } 
); 

它获取一些数据,然后运行在数据(if(this._jsnValService.valCategories(response)) {)验证。

然而,我的数据的验证实际上是异步过,因为它验证它针对JSON模式是在一个单独的JSON文件,因此必须首先读取文件。

我使用了一个承诺来读取文件内容,然后执行验证:

@Injectable() 
export class ValidateJSONSchemaService { 

    constructor(private http: Http) {} 

    public valCategories(json) { 
     this._getSchema("./jsonSchema.categories.json").then((schema) => { 
      this._valSchema(json, schema); 
     }); 
    }; 

    private _valSchema(json, schema): any { 
     var ajv = new Ajv(); 
     var valid = ajv.validate(schema, json); 
     if (!valid) { 
      console.log(ajv.errors); 
      return false; 
     } else { 
      console.log(valid); 
      return true; 
     }; 
    }; 

    private _getSchema(fileName): any { 
     return new Promise((resolve, reject) => { 
      this.http.get(fileName) 
       .map(this._extractData) 
       .catch(this._handleError) 
       .subscribe(schema => resolve(schema)); 
     }); 
    }; 

    private _extractData(res: Response) { 
     let body = res.json(); 
     return body.data || {}; 
    }; 

我怎么能在这个问题上编辑顶部代码块占if语句里面的异步函数(if(this._jsnValService.valCategories(response)) { )?

+0

一开始,你需要'valCategories'返回一个承诺,或接受回调参数 - 因为它是,你没有改变顶块使用该功能的机会 - 顺便说一下,什么语言是第二块?它@JaromandaX我使用打字稿不是JavaScript的 –

+0

。很抱歉,误导性的es6-promise标签。我明天将在工作中回到这个问题上,并尝试使用下面答案中的底部代码块。干杯 – BeniaminoBaggins

回答

0

如果你正在使用ES6你可以使用异步/等待是这样的:

async function _validateCategories() { 
    this.apiService.fetchCategories(!this.cacheData).subscribe(
    response => { 
     const valid = await this._jsnValService.valCategories(response) 
     if(valid) { 
      this.customerMap.categories = this.formatCategories(response["categories"]); 
     } else { 
      alert("Categories failed the schema validation. Please contact support if this happens again."); 
     } 
    }, 
    error => { 
     this.notification.title = "Oops, there's a problem."; 
     this.notification.content = "Seems there's an issue getting the provider categories."; 
     this.notification.show("provider_categories_api"); 
    } 
); 
} 

如果不是,你的函数fetchCategories应该返回一个承诺或允许您传递一个回调做这样的事情:

async function _validateCategories() { 
    this.apiService.fetchCategories(!this.cacheData).subscribe(
    response => { 
     this._jsnValService.valCategories(response).then((error, valid)=> { 
     if(error) { 
      alert("Categories failed the schema validation. Please contact support if this happens again."); 
     } 
     this.customerMap.categories = this.formatCategories(response["categories"]); 
     }) 
    }, 
    error => { 
     this.notification.title = "Oops, there's a problem."; 
     this.notification.content = "Seems there's an issue getting the provider categories."; 
     this.notification.show("provider_categories_api"); 
    } 
); 
}