本页尚未翻译,因此以英文显示。 参与翻译

pipe1

Applies one function to a value, awaiting it first if the value is a Future.

FutureOr<R> pipe1<A, R>(FutureOr<A> a, FutureOr<R> Function(A a) f)

Lecture

pipe1 is the single-step building block that pipe uses internally: apply f to a, but if a is a Future, await it first. If a is a plain value, f runs immediately and pipe1 returns whatever f returns — synchronously, with no Future wrapper at all.

The point is that f itself never has to know or care whether the value it receives came from sync or async work upstream — pipe1 normalizes that for you. This is handy when you're composing a value that's sometimes a Future (say, the result of an earlier async step) and you want the next step to look identical either way.

Because it only handles one step, you can nest pipe1 calls to compose a short chain, or reach for the full pipe (a List<Function>) when there are more than one or two steps.

Demo 1 · A plain (non-Future) value

With no Future involved, pipe1 just calls f(a) directly and returns a plain value:

Demo 2 · Awaiting a Future first

When a is a Future, pipe1 awaits it before calling f — chain a few of these together to compose async steps one at a time:

Try it yourself

Exercise: use pipe1 to turn a delayed name into a greeting.

Related: pipe — multi-step pipeline built from pipe1 · fx — the typed chain alternative · delay & sleep — the async helpers used above