Q:

Get the penultimate observable of a chained map() call in Rx Java

I have two observables:

Observable<User> createUser(UserData userData)
Observable<Void> loginUser(User user)

After the user creates a new account I wish to login with that new account. So I've done this:
createUser(userData).map(user -> loginUser(user));

But in that way, I get an Observable<Void> as return. What I want to do is to call createUser(userData).map(user -> loginUser(user)) and get an Observable<User> as return. Something like this:
@Override
public Observable<User> createUserAndLogin(){

    createUser(userData).map(user -> loginUser(user));

}

How could I do this?

A:

Why not use log-in as a side-effect with doOnNext?
createUser(userData).doOnNext(user -> loginUser(user));

If you want to make sure the log-in happened:
createUser(userData).flatMap(user -> {
    // attach a callback to the log-in and return
    // the user from the previous call
    return loginUser(user).map(dontCare -> user);
});

But I'm not sure how it's going to work if loginUser emits nothing, so perhaps forcibly concat the value you want to the empty loginUser observable:
 createUser(userData).flatMap(user -> {
    // attach a callback to the log-in and return
    // the user from the previous call
    return loginUser(user).concatWith(Observable.just(user));
});

