Skip to content

promiseChain

Chain async functions together

141 bytes
since v12.6.0

Usage

Creates a function that executes multiple functions in the same order as they are passed in arguments. Each function may be synchronous or asynchronous. The result of each function is passed to the next function. The final result is returned as a Promise.

import * as
import _
_
from 'radashi'
const
const func1: (a: number, b: number) => number
func1
= (
a: number
a
: number,
b: number
b
: number) =>
a: number
a
+
b: number
b
const
const func2: (n: number) => Promise<number>
func2
= async (
n: number
n
: number) =>
n: number
n
* 2
const
const func3: (n: number) => Promise<string>
func3
= async (
n: number
n
: number) => `Your Value is ${
n: number
n
}`
const
const chained: (a: number, b: number) => Promise<string>
chained
=
import _
_
.
function promiseChain<[a: number, b: number], number, number, string>(f1: (a: number, b: number) => _.Awaitable<number>, f2: (arg: number) => _.Awaitable<number>, f3: (arg: number) => _.Awaitable<string>): (a: number, b: number) => Promise<...> (+8 overloads)

Creates a function that executes multiple functions in the same order as they are passed in arguments. Each function may be synchronous or asynchronous. The result of each function is passed to the next function. The final result is returned as a Promise.

@seehttps://radashi.js.org/reference/curry/promiseChain

@example

const chained = promiseChain(
(x: number, y: number) => x + y
async (n: number) => n * 2
async (n: number) => `Your Value is ${n}`
)
await chained(2, 3) // "Your Value is 10"

@version12.6.0

promiseChain
(
const func1: (a: number, b: number) => number
func1
,
const func2: (n: number) => Promise<number>
func2
,
const func3: (n: number) => Promise<string>
func3
)
await
const chained: (a: number, b: number) => Promise<string>
chained
(5, 2) // => "Your Value is 14"