Class PojoConstructorSyncAndAsync<Pojo, CtorInput>

Can operate in both sync mode and async mode.
Constructor methods for each of properties returns an object with either one of sync, async methods or both.

All of these are valid:

  • { sync, async }.
  • { sync }.
  • { async }.

Where

  • async - returns promise for { value } object
  • sync - returns { value } object synchronously

If you only specify sync methods, you can use them for "async mode" (calling PojoConstructorSyncAndAsync#new().async()), but you cannot use "sync mode" (calling PojoConstructorSyncAndAsync#new().sync()) if you only specify async methods.

You can specify async methods for some fields and still construct an object in "sync mode" if you also specify a catch option. catch will be called each time constructing a property fails, but all properties that do not fail will be added to resulting object.

Usage

// Sync mode
const ctor = new PojoConstructor<{ field: number }, number>({ field: (input) => ({ sync: () => ({ value: input + 2 })}) })
const obj = ctor.pojo(2).sync();
assert.strictEqual(obj.field, 4);
// Async mode
const ctor = new PojoConstructor<{ field: number }, number>({ field: (input) => ({ async: () => ({ value: input + 2 })}) })
const obj = await ctor.pojo(2).async();
assert.strictEqual(obj.field, 4);

Type Parameters

  • Pojo extends object

  • CtorInput = unknown

Hierarchy

  • PojoConstructorSyncAndAsync

Constructors

Properties

options?: PojoConstructorSyncAndAsyncOptions<Pojo, CtorInput>
props: PojoConstructorSyncAndAsyncProps<Pojo, CtorInput>

Methods

Generated using TypeDoc