2017-09-13 49 views
0

我有一个函数如何退还承诺?

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; 
} 

如何返回结果的承诺,我尽力了,但我不知道该怎么做T,也许它因为两个观察的我有它的内部,

+1

可能重复的[如何从异步调用返回响应?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-调用) –

+0

你使用_Reactive-Extensions(RxJS)_库吗? – alexmac

+0

是的,我正在使用 –

回答

1

你会promisify两个观测,然后用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方法来获得结果。

0

也许你需要类似的东西:

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; 
} 
0

您有2个异步调用。因此,基于这些知识,还有一个单一的承诺解决方案。

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); 
     }); 
    }); 

    }); 

你也可以看看Promise.all方法。