Javascript 如何回报承诺?
我有一个函数Javascript 如何回报承诺?,javascript,typescript,promise,es6-promise,Javascript,Typescript,Promise,Es6 Promise,我有一个函数 parseJobs(userId: string) { this.getLikedJobs(userId).subscribe(result => { result.map(key =>{ let rows = { name : (key as any).jobsUser.firstName, jobType: 'Liked' }
parseJobs(userId: string) {
this.getLikedJobs(userId).subscribe(result => {
result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Liked'
}
let job = {...rows,...(key as any).jobPosting};
this.result.push(job);
});
});
this.getSavedJobs(userId).subscribe(result => {
result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Saved'
}
let job = {...rows,...(key as any).jobPosting};
this.result.push(job);
});
});
return this.result;
}
如何将结果回报给承诺,我尽了最大努力,但我不知道该怎么做,可能是因为我有两个可以观察到的结果,也许你需要这样的东西:
parseJobs(userId: string): Promise<any> {
let res: Function;
const resPromise = new Promise((resolve: Function) => {
// passing resolve function to upper scope
res = resolve;
});
this.getLikedJobs(userId).subscribe(result => {
...
// resolving result promise
res(result);
});
...
// unresolved
return resPromise;
}
parseJobs(userId:string):Promise{
let res:函数;
const resPromise=新承诺((解析:函数)=>{
//将解析函数传递给上层作用域
res=决心;
});
this.getLikedJobs(userId).subscribe(结果=>{
...
//解决结果承诺
res(结果);
});
...
//悬而未决
回报再承诺;
}
您将承诺这两个可观测值,然后使用Promise.all
获得一个在完成所有操作后实现的承诺:
parseJobs(userId: string) {
// Create a promise
const p1 = new Promise(resolve => {
this.getLikedJobs(userId).subscribe(result => {
// Resolve with the modified array
resolve(result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Liked'
}
let job = {...rows,...(key as any).jobPosting};
// In a map, you want to return:
return job;
}));
});
});
// Same here:
const p2 = new Promise(resolve => {
this.getSavedJobs(userId).subscribe(result => {
resolve(result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Saved'
}
let job = {...rows,...(key as any).jobPosting};
return job;
}));
});
});
// Return a promise that will fulfill when both promises fulfill
// and concatenate the results
return Promise.all([p1, p2]).then(result => [].concat(...result));
}
现在,您不再将结果存储在this.result中,而是将其设置为承诺值,如下所示:
parseJobs(1).then(result =>
console.log(result);
});
当然,您仍然可以将结果存储在
this.result
中,但这不是最佳做法,因为它表明一段代码可能会在可用之前尝试访问它:您将始终使用then
方法来获得结果。您有两个异步调用。因此,也有一个基于此知识的单一承诺解决方案
parseJobs(userId: string) =>
new Promise(resolve => {
let result = [];
const done = (job) => {
result.push(job);
if(result.length === 2) {
resolve(result);
}
}
this.getLikedJobs(userId).subscribe(result => {
result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Liked'
}
let job = {...rows,...(key as any).jobPosting};
done(job);
});
});
this.getSavedJobs(userId).subscribe(result => {
result.map(key =>{
let rows = {
name : (key as any).jobsUser.firstName,
jobType: 'Saved'
}
let job = {...rows,...(key as any).jobPosting};
done(job);
});
});
});
您还可以查看方法。是否可能重复使用被动扩展(RxJS)库?是的,我正在使用