Mono.Defer() vs Mono.create() vs Mono.just()? [英] Mono.Defer() vs Mono.create() vs Mono.just()?

查看:583
本文介绍了Mono.Defer() vs Mono.create() vs Mono.just()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮我理解 Mono.defer()、Mono.create() 和 Mono.just() 之间的区别吗?如何正确使用?

Could someone help me to understand the difference between Mono.defer(), Mono.create() and Mono.just()? How to use it properly?

推荐答案

Mono.just(value) 是最原始的——一旦你有了一个值,你就可以把它包装成 Mono 并订阅者下来该行会得到它.

Mono.just(value) is the most primitive - once you have a value you can wrap it into a Mono and subscribers down the line will get it.

Mono.defer(monoSupplier) 允许您提供提供结果 Mono 实例的整个表达式.这个表达式的计算被推迟到有人订阅.在这个表达式中,您还可以使用像 Mono.error(throwable) 这样的控制结构来表示错误条件(您不能使用 Mono.just 来做到这一点).

Mono.defer(monoSupplier) lets you provide the whole expression that supplies the resulting Mono instance. The evaluation of this expression is deferred until somebody subscribes. Inside of this expression you can additionally use control structures like Mono.error(throwable) to signal an error condition (you cannot do this with Mono.just).

Mono.create(monoSinkConsumer) 是最先进的方法,可让您完全控制发出的值.不需要从回调中返回 Mono 实例(如在 Mono.defer 中),您可以控制 MonoSink允许您通过 MonoSink.success()MonoSink.success(value)MonoSink.error(throwable) 方法发出值.Reactor 文档包含一些可能的 Mono.create 用例的好例子:链接到文档.

Mono.create(monoSinkConsumer) is the most advanced method that gives you the full control over the emitted values. Instead of the need to return Mono instance from the callback (as in Mono.defer), you get control over the MonoSink<T> that lets you emit values through MonoSink.success(), MonoSink.success(value), MonoSink.error(throwable) methods. Reactor documentation contains a few good examples of possible Mono.create use cases: link to doc.

一般建议是使用最不强大的抽象来完成这项工作:Mono.just ->Mono.defer ->Mono.create.

The general advice is to use the least powerful abstraction to do the job: Mono.just -> Mono.defer -> Mono.create.

这篇关于Mono.Defer() vs Mono.create() vs Mono.just()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆