[英]Return custom type in Promise
I had some JSON array like below and I got these JSON from service. 我有一些如下所示的JSON数组,并且从服务中获取了这些JSON。
[{
"id": 3,
"name": "Arun"
}, {
"id": 5,
"name": "Barun"
}, {
"id": 1,
"name": "Joy"
}, {
"id": 8,
"name": "Suman"
}]
Now, I made a like below 现在,我在下面做了一个
class Person{
id:number
name:string
deserialize(input) {
this.id = input.id;
this.name = input.name
return this;
}
}
Now, I had a service class from where I am getting JSON(Array/dictionary) with Promise and I send this Promise to my parsing function. 现在,我有了一个服务类,从那里可以通过Promise获取JSON(Array / dictionary),并将该Promise发送到解析函数。 My Parsing function is below 我的解析功能如下
private parse(response:Promise<{}>):Promise<{}>{
return new Promise((success,failure) =>{
response.then((result) => {
if (result instanceof Array || Array.isArray(result)){
let rs1 = result.map((val) => new Person().deserialize(val))
success(rs1);
}else{
success(new Person().deserialize(result))
}
},(err) => {
failure(err)
});
});
}
Now, I have two function 现在,我有两个功能
expectingObjectOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let person = response as Person
//log name of this person
console.log(person.name) //no run time exception
//log name of this person
console.log(person.nam) //Error thrown by compiler
//now if I do this
console.log(response.name) //no run time exception
//if this
console.log(response.nam) //run time exception, also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to Person */
}, (error) => {
Console.log(error);
})
}
expectingArrayOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let persons = response as Person[]
//log name of this person
console.log(persons.map((val) => val.name)) //no run time exception
//log name of this person
console.log(persons.map((val) => val.name)) //Error thrown by compiler
//now if I do this
console.log(response.map((val) => val.name)) //no run time exception
//if this
console.log(persons.map((val) => val.name)) //run time exception , also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to array of Person */
}, (error) => {
Console.log(error);
})
}
Is there any way that response could automatically inferred to Person object or array of Person depends on response ? 有什么方法可以将响应自动推断为Person对象或Person数组取决于响应? I am new in Angular, any help will be appreciated. 我是Angular的新手,任何帮助将不胜感激。
I am using latest version of Typescript. 我正在使用最新版本的Typescript。
First, let's go over your service : you're creating a Promise, to handle another promise. 首先,让我们来看一下您的服务:您正在创建一个Promise,以履行另一个承诺。 That's repeating yourself. 在重复自己。
Furthermore, you should use Observables, which are upgraded promises. 此外,您应该使用Observables,它们是已升级的Promise。
Finally, you're creating a new Person object everytime. 最后,您每次都会创建一个新的Person对象。 Consider using a static method to deserialize. 考虑使用静态方法反序列化。
This gives you : 这给您:
export function parse(response: Promise<{}>): Observable<{}> {
// Rx 6 : return fromPromise(response)
return Observable.fromPromise(response)
.map(result => {
if (result instanceof Array || Array.isArray(result)) {
return result.map((val) => Person.deserialize(val))
} else {
return Person.deserialize(result);
}
})
}
export class Person {
id: number;
name: string;
public static deserialize(input: any): Person {
const person = new Person();
person.id = input.id;
person.name = input.name;
return person;
}
}
Now you can use your two functions like this : 现在,您可以像这样使用两个函数:
expectingObjectOfPerson(){
this.parse(servicePromise) // Whatever it is
.subscribe(personInstance => {
console.log(personInstance);
console.log(personInstance instanceof Person);
}, error => console.log(error));
}
expectingArrayOfPerson(){
this.parse(servicePromise) // Whatever it is
.subscribe(persons => {
for (const personInstance of persons) {
console.log(personInstance);
console.log(personInstance instanceof Person);
}
}, error => console.log(error));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.