rxjs6不在管道内的promise中传递值

时间:2019-10-23 14:02:27

标签: javascript rxjs

我有一段这样的代码:

source$.pipe(
    exhaustMap(input => ajaxPost1(input)), // ajaxPost1 returns ajaxObservable
    tap(console.log), // can print HTTP response
    exhaustMap(({result}) => ajaxPost2(result)), // ajaxPost2 returns promise
    tap(console.log), // print nothing sometimes even if promise has been resolved and I don't know why
)
.subscribe(...);

ajaxPost2实际上是一个名为handlePrecheck的函数,如下所示:

handlePrecheck(precheckResponse) {
    const {response, request} = precheckResponse;

    if (!response.status) {
        this.displayInfo(response.msg);
        return Promise.resolve(null);
    }

    // 早退时,签到流程是否继续由UI决定
    if (response.isEarlyCheckout) {
        // const a = this.confirmEarlyCheckout(precheckResponse);
        // window.a = a;
        // console.log(a);
        // return a;
        return this.confirmEarlyCheckout(precheckResponse);
    }

    if (response.holidayToCancel && response.holidayToCancel.id) {
        const {id, duration} = response.holidayToCancel

        this.confirmCancelHoliday(request.body.get('picture'), duration, id);
        // 当有假期时总是终止签到过程
        return Promise.resolve(null);
    }

    return Promise.resolve(precheckResponse);
}

this.confirmEarlyCheckout是一种返回promise的方法,该方法将根据UI事件立即解决(我可以通过在开发工具中检查promise对象来确认这一点)。

handlePrecheck仅在进入if(response.isEarlyCheckout)分支时才将值传递给下游,但在进入其他分支并返回promise时效果很好。

非常感谢任何人能提供帮助。

1 个答案:

答案 0 :(得分:0)

好吧...我已经找到原因了……我在等待UI事件解决Promise时取消订阅source$,因此Promise的已解决值不会传递到下游 >