gpt4 book ai didi

javascript - 用于稍后提取值的可观察运算符

转载 作者:行者123 更新时间:2023-12-01 02:21:03 25 4
gpt4 key购买 nike

我正在将可观察量与 RXJS 库一起使用,并且遇到一种情况,我想提取一个值并在未来某个未指定的时间(不是下一个运算符或订阅方法)使用它。

我目前正在通过每个操作传递该值,直到我需要它为止。看起来像这样的东西:

obsveable$.pipe(
ofType('example'),
map((action) => [
action, // <-- passing this through
somethingElse
]),
map(([action, other]) => [
action, // <-- through again
anotherThing
]),
map(([action, other]) => {
// finally use action
}),
);

我也许可以尝试将其保存到局部变量中 - 像这样:

let action;
obsveable$.pipe(
ofType('example'),
map((_action) => action = _action),
map((action) => action.somethingElse),
map((other) => {
// finally use action
})
);

这两种解决方案都不是最优的。当有很多步骤时,第一个会变得笨拙,第二个可能会出现操作顺序问题(并且它没有使用很酷的 react 方法)。

还有哪些其他选择?

最佳答案

您可以通过以下方式创建一个闭包:

obsveable$.pipe(
ofType('example'),
mergeMap(action =>
Observable.of(somethingElse)
.pipe(
map(other => anotherThing),
map(other => {
// use action
})
)
)
);

关于javascript - 用于稍后提取值的可观察运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212159/

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