2

I'm currently using pipe and concatMap to chain multiple http requests like this:

onErrorResumeNext(from(ids))
    .pipe(
        delay(10),
        concatMap(id => get(id))
    )
    .subscribe(res => {
        if (res = emptydata) return id
});

The get function handles building the query string from an array of ids and also contains retry and catchError.

Now, the problem is I need to access the same id parameter if I get no data on the response object.

Any ideas?

4
  • Where do you need to access the id parameter? Commented Sep 26, 2018 at 17:46
  • After subscription. It's not really an http error because I do get a response. It's just an empty object. I have an if-else statement after subscription that can check for that. If true, I need to find which ID it was. Commented Sep 26, 2018 at 17:56
  • What do you mean by "after subscription?" You can't do anything after the subscription since it's asynchronous. You would have to handle it within the subscription callback. Commented Sep 26, 2018 at 18:27
  • Yes, the subscription callback once the response comes back. I've edited the question. I'm not sure if this might affect it, but the subscription happens in the component, however, the pipe is returned from a service. Commented Sep 26, 2018 at 18:33

1 Answer 1

1

concatMap (and other higher order Observable operators) take an optional result selector function (Note: this is deprecated) that takes the output value and input value arguments. This allows you to pass both to the stream (for example as an array):

onErrorResumeNext(from(ids))
    .pipe(
        delay(10),
        concatMap(id => get(id), (res, id) => ([res, id]))
    )
    .subscribe(([res, id]) => {
        // note that `return` does nothing here
        if (res == emptydata) return id
});

If get is an Observable, you could also return id in the context where you have access to it:

concatMap(id => get(id).pipe(
  map(res => res != emptydata ? res : id)
)

If get is not an array, you can use an Observable creation function such as from to turn it into an array.

Sign up to request clarification or add additional context in comments.

6 Comments

Will try this out. Thanks!
Notice that the result selector was deprecated on most operators since v5/v6. Please consider this in your answer.
@Jota.Toledo I wasn't aware of the resultSelector deprecation; thank you for pointing that out. I don't know another way to handle this other than my other suggestion of working within the concatMap callback function context.
I read about the resultSelector and its deprecation. Did not really understood how/when to use it before. However, since it's working for me. I'd use it while I can.
@JignoAlfredVenezuela github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/… explains how to migrate, and my second code example is specific to your case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.