- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在android中使用rxjava2,有时会遇到这样的问题:
Observable.fromArray(
// maybe a list about photo url in SDCard
)
.flatMap(
// make list to single and upload to server,
// use retrofit observable
)
.map(uploadResponse -> {
// *Problem of point
})
.map(
// in this map I want to get this single photo url in SDCard
// and some info from uploadResponse,how to do in
// *Problem of point of previous map?
);
这段代码忽略了一些线程切换和其他不重要的步骤。这个问题也让我对nodejs的Promise感到困惑,如何向链的每一步传递一些值?我在firebase上使用nodejs的版本6,它不支持await。
1 Câu trả lời
对于问题的JavaScript部分,可以使用async..await
方便地解决:
(async () => {
const foo = await fooPromise;
const bar = await getBarPromise(foo);
const baz = await getBazPromise(bar);
})()
.catch(console.error);
I use nodejs's version6 on firebase,it not support await.
自从 Node 4 中生成器出现以来,async..await
背后的模式就被广泛使用。同样的事情可以使用 co
来实现,这是最著名的实现:
const co = require('co');
co(function * () {
const foo = yield fooPromise;
const bar = yield getBarPromise(foo);
const baz = yield getBazPromise(bar);
})
.catch(console.error);
由于 không đồng bộ
函数是 Promise 的语法糖,因此 không đồng bộ
函数所做的一切都可以单独用 Promise 重写。
假设有一个稍后可能需要的值(đồ ăn
):
fooPromise
.then(foo => getBarPromise(foo))
.then(bar => getBazPromise(bar))
.then(baz => {
// foo and baz are needed here
});
它应该与其他结果一起通过链传递:
fooPromise
.then(foo => {
return getBarPromise(foo).then(bar => ({ foo, bar }));
})
.then(({ foo, bar }) => {
return getBazPromise(bar).then(baz => ({ foo, baz }));
})
.then(({ foo, baz }) => {
// foo and baz are destructured from the result
});
或者 Promise 应该嵌套,以便 đồ ăn
在当前函数作用域内可用:
fooPromise
.then(foo => {
return getBarPromise(foo)
.then(bar => getBazPromise(bar))
.then(baz => {
// foo and baz are available here
})
});
相同的方法适用于其他 API 和语言。例如,RxJS:
fooObservable
.flatMap(foo => Observable.zip(
Observable.of(foo),
getBarObservable(foo)
))
.map(([foo, bar]) => {
// foo and bar are destructured from the result
})
关于node.js - Rxjava2如何简洁地将结果从前一个 Node 返回到链中的下一个 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51687404/
我正在用 C# 编写动态语言的解释器,并将原始函数实现为具有虚拟 Apply 方法的抽象类 Primitive,其中每个实际原始函数都是重写 Apply 的子类。 (另一种方法是只拥有类 Primit
我正在用 C# 编写动态语言的解释器,并将原始函数实现为具有虚拟 Apply 方法的抽象类 Primitive,其中每个实际原始函数都是重写 Apply 的子类。 (另一种方法是只拥有类 Primit
我是 Dapper 的新手我正在尝试了解它实际上是如何映射事物的。我有以下数据库结构: calendar | Id | Name | meeting_room | Id | Calendar_id
抱歉问题标题很糟糕。有没有办法在一行中做到这一点: Button button = (Button)Gridview.Cells[0].FindControl("controlname"); butt
在 Java 中在声明点和使用点声明列表/数组文字的tersest方法是什么? 作为次要问题,我更喜欢一种不会导致编译时警告或要求抑制警告的方法。 注意:就我个人而言,这是针对Java 8ish on
什么是现代、简洁、快速的方法来测试节点是否有任何与给定选择器匹配的子节点? “简洁”是指类似于 jQuery 或函数式风格,例如避免循环。我知道本地选择器越来越多地使用这种类型的东西,但没有跟上发展的
getFirstNotNullResult 执行函数列表,直到其中一个函数返回非空值。 如何更优雅/简洁地实现 getNotNullFirstResult? object A { def main
根据 stackoverflow 上某人的推荐,我使用了 jquery succint https://github.com/micjamking/Succinct截断我在 php 网站上的帖子。 它
Tôi là một lập trình viên xuất sắc, rất giỏi!