Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何回报承诺?_Javascript_Typescript_Promise_Es6 Promise - Fatal编程技术网

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)库?是的,我正在使用