链接RXJ承诺从Observable.from

时间:2019-04-23 14:25:56

标签: rxjs rxjs6

在RxJS 6中,如何使用诺言链接和传递数据?我必须使用jira-connector npm库连续执行一堆JIRA api调用。但是我不确定如何执行数据并将其传递给函数。

谢谢!

例如:

    const pipeData = Observable.from(jira.search.search({
        jql: 'team = 41 and type = "Initiative"'
    }))

    pipeData.pipe(
        data => operators.flatMap(data.issues),
        issue => Observable.from(jira.search.search({
        jql: `team = 41 and "Parent Link" = ${issue.key}`
    }))).subscribe(results => {
        console.log(results)
    })

1 个答案:

答案 0 :(得分:1)

首先,必须在管道函数中使用 lettable 运算符。

您要尝试执行的操作是:

  1. 打个电话,得到一系列问题;
  2. 每个问题都要单独致电;
  3. 获取结果

类似这样:

pipeData.pipe(

    // when pipeData emits, subscribe to the following and cancel the previous subscription if there was one:
    switchMap(data => of(data.issues))),

    // now you get array of issues, so concatAll and get a stream of it:
    concatAll(),

    // now to call another HTTP call for every emit, use concatMap:
    concatMap(issue => jira.search.search({
    jql: `team = 41 and "Parent Link" = ${issue.key}`
})).subscribe(results => {
    console.log(results)
})

请注意,我没有将jira.search.searchfrom包裹起来,因为您还可以将promise传递到concatMap中,还可以传递第二个参数- resultSelector 函数,仅在需要时选择一些属性:

concatMap(issue => jira.search.search({
        jql: `team = 41 and "Parent Link" = ${issue.key}`),
        result => result.prop1
)