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

前端 未结 1 573
北恋
北恋 2020-12-07 00:20

I was under impression that Observable.[prototype.]concat makes sure first operation is fully finished before second operation starts. But in following code:

相关标签:
1条回答
  • 2020-12-07 00:48

    The problem is that fromNodeCallback creates a function that when invoked executes the underlying function and returns the result of the invocation through an Observable. Essentially the Observable return value is replacing the node style callback you would normally have to pass as the last argument to the function. However, the function still gets invoked immediately.

    If you want to delay the execution of the methods you can wrap them in defer to prevent their execution until the Observables are subscribed to.

    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$
        );
    
    0 讨论(0)
提交回复
热议问题