gpt4 book ai didi

javascript - 如何在 promise 中使用 withLatestFrom?

转载 作者:行者123 更新时间:2023-12-04 08:29:08 33 4
gpt4 key购买 nike

为什么不能转换 promiseobservable然后与 withLatestFrom 一起使用.我不清楚,为什么这不能解决。

of('')
.pipe(
withLatestFrom(from(myPromise)),
map((res) => {
console.log('does not resolve', res);
})
)
.subscribe();

最佳答案

WithLatestFrom仅在其源 Observable 发出时才发出,因为 of('')在源之前发射到 withLatestFrom,你的 withLatestFrom永远不会被触发。
以下代码将不起作用:

of('1 + 1')
.pipe(
withLatestFrom(new Promise(x => x("== 2"))),
map(([res1,res2]) => console.log(res1, res2))
)
.subscribe();
但这将:
of('1 + 1')
.pipe(
delay(100), // <= delay source execution
withLatestFrom(new Promise(x => x("== 2"))),
map(([res1,res2]) => console.log(res1, res2))
)
.subscribe();

关于javascript - 如何在 promise 中使用 withLatestFrom?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65111311/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com