A callback that receives the Future's FutureId, ResolveCallback, and RejectCallback.
A Future.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
// Sends an email and returns a Future that resolves with its response.
function sendEmail(
to: string,
subject: string,
body: string
): Future<string> {
return futures.create<string>((id, _resolve, _reject) => {
emailService.send({
to,
subject,
body,
metadata: JSON.stringify({ id }),
});
});
}
// Email may be received on another session.
emailService.onEmail((response: string, metadata: string) => {
const futureId = JSON.parse(metadata).id as FutureId<string>;
futures.resolveFutureById(futureId, response);
});
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future and returns it, its ResolveCallback, its RejectCallback, and its FutureId.
A Future, its ResolveCallback, its RejectCallback, and its FutureId.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
// Sends an email and returns a Future that resolves with its response.
function sendEmail(
to: string,
subject: string,
body: string
): Future<string> {
const { future, id } = futures.withResolvers();
emailService.send({
to,
subject,
body,
metadata: JSON.stringify({ id }),
});
return future;
}
// Email may be received on another session.
emailService.onEmail((response: string, metadata: string) => {
const futureId = JSON.parse(metadata).id as FutureId<string>;
futures.resolveFutureById(futureId, response);
});
Actual Type: Method
Note: Stripped to generate better documentation.
The value to resolve the Future with.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
// Email may be received on another session.
emailService.onEmail((response: string, metadata: string) => {
const futureId = JSON.parse(metadata).id as FutureId<string>;
futures.resolveFutureById(futureId, response);
});
// Sends an email and returns a Future that resolves with its response.
function sendEmail(
to: string,
subject: string,
body: string
): Future<string> {
const { future, id } = futures.withResolvers();
emailService.send({
to,
subject,
body,
metadata: JSON.stringify({ id }),
});
return future;
}
Actual Type: Method
Note: Stripped to generate better documentation.
Optionalreason: SerializableThe value to reject the Future with.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
// Email may be received on another session.
emailService.onEmailDropped((metadata: string) => {
const futureId = JSON.parse(metadata).id as FutureId<string>;
futures.rejectFutureById(futureId, exceptions.createException('Email dropped.'));
});
// Sends an email and returns a Future that resolves with its response.
function sendEmail(
to: string,
subject: string,
body: string
): Future<string> {
const { future, id } = futures.withResolvers();
emailService.send({
to,
subject,
body,
metadata: JSON.stringify({ id }),
});
return future;
}
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future resolved with result.
The value to resolve the Future with.
A Future resolved with result.
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future rejected with reason.
Optionalreason: SerializableThe value to reject the Future with.
A Future rejected with reason.
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future that's resolved with method's return value or
rejected with the value it throws.
A Future either resolved with the value returned by
method or rejected with the value thrown by method.
const { methods, exceptions } = createFutureMachine(futureDatabase);
const divideMethod = methods.create(
'divideMethod',
(numerator: number, denominator: number) => {
if (denominator === 0) {
throw exceptions.createException('Divide by zero');
}
return numerator / denominator;
}
);
const futures = methods.build();
const resolvedFuture = futures.try(divideMethod, 10, 5);
const rejectedFuture = futures.try(divideMethod, 7, 0);
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future that settles when the first Future in
values settles.
An iterable of Serializables.
A Future which settles with the same outcome as the first
settled value in values.
The returned Future will either be:
values are unsettled Futuresconst { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
const { future: f1, resolve: r1 } = futures.withResolvers<string>();
const { future: f2, resolve: r2 } = futures.withResolvers<number>();
const raceFuture: Future<string | number> = futures.race([f1, f2]);
r2(4312);
r1('Hello');
// raceFuture will be resolved with 4312.
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future that resolves when all values are resolved, or
rejects as soon as any Future in values rejects.
An iterable of Serializables.
A Future which resolves with a List of the results
of values or rejects with the first Future in values to reject.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
const { future: f1, resolve: r1 } = futures.withResolvers<string>();
const { future: f2, resolve: r2 } = futures.withResolvers<number>();
const allFuture: Future<List<[string, number]>> = futures.all([f1, f2]);
r2(1234);
r1('Hello');
// allFuture resolves with List<['Hello', 1234]>.
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future that resolves with the first successfully resolved value.
An iterable of Serializables.
A Future that resolves when any value in values is
resolved or rejects with an AggregateException when all values
are rejected.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
const { future: f1, reject: reject1 } = futures.withResolvers<string>();
const { future: f2, resolve: resolve2 } =
futures.withResolvers<number>();
const anyFuture: Future<string | number> = futures.any([f1, f2]);
reject1('Hello');
resolve2(4312);
// anyFuture is resolved with 4312.
Actual Type: Method
Note: Stripped to generate better documentation.
Creates a Future that resolves when all values are settled.
An iterable of Serializables.
A Future that resolves to a List of
FutureSettledResults containing the resolution information of each
value in values.
const { methods } = createFutureMachine(futureDatabase);
const futures = methods.build();
const { future: f1, resolve: resolve1 } =
futures.withResolvers<string>();
const { future: f2, reject: reject2 } = futures.withResolvers<number>();
const allSettledFuture: Future<
List<[FutureSettledResult<string>, FutureSettledResult<number>]>
> = futures.allSettled([f1, f2]);
reject2(1234);
resolve1('Hello');
// `allSettledFuture` is resolved with a List containing
// FutureSettledResults with these values:
// * [0]: { status: 'fulfilled', value: 'Hello' }
// * [1]: { status: 'rejected', reason: 1234 }
The API for Futures.
The Serializable equivalent of a vanilla JavaScript
Promise.