Graeme
Graeme

Reputation: 1117

Chaining RXJs promises Observable.from

In RxJS 6, how can I chain and pass data with promises? I have to do a bunch of JIRA api calls in a row using the jira-connector npm library. But I'm not sure how to execute and pass the data to functions.

Thanks!

Ex:

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

Upvotes: 0

Views: 53

Answers (1)

Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

In the first place, you must use lettable operators in pipe function.

What appears you are trying to do is to:

  1. Make one call and get array of issues;
  2. Make separate call for every issue;
  3. Get results

So something like:

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

Note that I didn't wrapped jira.search.search with from as you can also pass promises into concatMap and you can also pass a second param - resultSelector function to select just some of the properties, if needed:

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

Upvotes: 1

Related Questions