How to 'wait' for two observables in RxJS

前端 未结 7 1538
温柔的废话
温柔的废话 2020-12-13 12:14

In my app i have something like:

this._personService.getName(id)
      .concat(this._documentService.getDocument())
      .subscribe((response) => {
              


        
7条回答
  •  自闭症患者
    2020-12-13 12:40

    You can use 'zip' or 'buffer' like the following.

    function getName() {
        return Observable.of('some name').delay(100);
    }
    
    function getDocument() {
        return Observable.of('some document').delay(200);
    }
    
    // CASE1 : concurrent requests
    Observable.zip(getName(), getDocument(), (name, document) => {
        return `${name}-${document}`;
    })
        .subscribe(value => console.log(`concurrent: ${value}`));
    
    // CASE2 : sequential requests
    getName().concat(getDocument())
        .bufferCount(2)
        .map(values => `${values[0]}-${values[1]}`)
        .subscribe(value => console.log(`sequential: ${value}`));
    

提交回复
热议问题