【问题标题】:Chaining RXJs promises Observable.from链接 RXJ 承诺 Observable.from
【发布时间】:2019-04-23 23:41:20
【问题描述】:

在 RxJS 6 中,如何使用 Promise 链接和传递数据?我必须使用 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)
    })

【问题讨论】:

    标签: rxjs rxjs6


    【解决方案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 包装在一起,因为您也可以将承诺传递给 concatMap,您还可以传递第二个参数 - resultSelector 函数来选择其中的一些属性(如果需要):

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

    【讨论】:

    • 谢谢你的工作。我来自 RxJava/Swift,所以这与我通常的想法有点不同!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 2023-01-27
    • 2015-01-21
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2016-06-21
    相关资源
    最近更新 更多