How to start second observable * only * after the first * completely * done in rxjs

My impression is that it Observable.[prototype.]concat

ensures that the first operation is completely finished before the second operation begins. But in the following code:

Observable
    .concat(
        Observable.fromNodeCallback(rimraf)(path.resolve('./some_dir')),
        Observable.fromNodeCallback(mkdir)(path.resolve('./some_dir')),
        writeToSomeDir$
    )

      

mkdir

tries (and fails) to create ./some_dir

before rimraf

finishing deleting the directory. However, at the end, (throwing) ./some_dir

ends with a removal.

Why Observable.concat

does it show this behavior? How can I make sure the first Observable is completely finished before starting with the second Observable without making the rimraf version sync?

+1


source to share


1 answer


The problem is that it fromNodeCallback

creates a function that, when called, executes the underlying function and returns the result of the call with Observable

. Essentially, the return value Observable

replaces the node style callback you would normally pass as the last argument to the function. However, the function is still called immediately.

If you want to defer the execution of methods, you can wrap them in defer

to prevent them from being executed until Observables

they are signed.



var rimrafObservable = Observable.fromNodeCallback(rimraf);
var mkdirObservable = Observable.fromNodeCallback(mkdir);

Observable
    .concat(
        Observable.defer(() => rimrafObservable(path.resolve('./some_dir'))),
        Observable.defer(() => mkdirObservable(path.resolve('./some_dir'))),
        writeToSomeDir$
    );

      

+2


source







All Articles