question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to chain calls, depending on result ?

See original GitHub issue

Hello,

In my webapp, I have to chain two async calls : The first call gives me a list of dog ids. The second call gives me a Dog (found by its id).

In my code, i’d like to write something like :

final Observable<RxMessage<String>> observable1 = asyncCallForDogIds(..);

final List<Dog> dogs = new ArrayList<>();
observable1.subscribe(
    // onNext
    (RxMessage<String> message) -> {
        final String id = readId(message);

        final Observable<RxMessage<String>> observable2 = someAsyncCallForOneDogId(id, ..);

        final Dog dog = readFromSomeMessage2(...);
        dogs.add(dog);
    },

    // onError
    (Throwable err) -> error(err, ..),

    // onCompleted
    () -> resume(dogs, ..));

What would be the best way to do that ? Th.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
benjchristensencommented, Dec 9, 2014

If you want to handle errors for each call you put the onErrorResumeNext or similar handlers on each of the async calls.

asyncCallForDogIds(..)
    .map(this::readId)
    .flatMap(dogId -> {
             return someAsyncCallForOneDogId(id, ..)
                             .onErrorResumeNext(errorHandlingHere)
    })
    .map(this::readFromSomeMessage2)
1reaction
hamidpcommented, Dec 8, 2014

You want flatMap so roughly:

asyncCallForDogIds(..)
    .map(this::readId)
    .flatMap(dogId -> someAsyncCallForOneDogId(id, ..))
    .map(this::readFromSomeMessage2)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promises chaining - The Modern JavaScript Tutorial
The idea is that the result is passed through the chain of .then handlers. Here the flow is: The initial promise resolves in...
Read more >
How to proper chain promises calls that depend on one ...
Basically, it's a chain of request call, each one based on the result of the previous call. The problem is that I have...
Read more >
Chain HTTP Requests in React Using Promises - Pluralsight
Chaining Requests Using Promises​​ You can easily complete this feature by using the Fetch and Promise APIs. The below code is an example...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs
The optional chaining ( ?. ) operator accesses an object's property or calls a function. If the object is undefined or null ,...
Read more >
Chaining tasks using continuation tasks | Microsoft Learn
Learn to chain task by using continuation tasks in .NET. A continuation task is an ... Result property, which does block the calling...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found