Skip to content

Async morphism #106

@SamuelColacchia

Description

@SamuelColacchia

Is your feature request related to a problem? Please describe.
Morphism is unable to resolve promises in ActionFunction and ActionSelector and Type Promise is not allowed.

Describe the solution you'd like
Allow ActionFunction and ActionSelector to return a Promise that would later be resolvable by say a morphismasync method.

Example Schema

// Schema type removed to allow for group to be of type promise
const customerSchema = {
  firstname: "firstname",
  lastname: "lastname",
  email: "email",
  group: {
    path: "group",
    fn: async value => {
      const res = await callSomeService(value);
      return res.data;
    }
  }
};

Describe alternatives you've considered
Alternative solutions would be to map the data available with one morphism call then make any async requests desired then remap the data returned from those async requests.

Another solution would be to add a helper function like so.

async function resolvePromises(objectFromMorph) {
  for (const key of Object.keys(objectFromMorph)) {
    const value = objectFromMorph[key];
    if (Promise.resolve(value) == value) {
      console.log("Found Promise", value);
      objectFromMorph[key] = await value;
    }
  }
  return objectFromMorph;
}
const morphed = morphism(customerSchema, customer.data);
const customer = resolvePromises(morphed);
{
  "sourceCustomerId": 39392,
  "firstname": "somefirstname",
  "lastname": "somelastname",
  "email": "mail@mail.com",
  "group": "Promise { <pending> }"
}
// After resolvePromises
{
  "sourceCustomerId": 39392,
  "firstname": "somefirstname",
  "lastname": "somelastname",
  "email": "mail@mail.com",
  "group": "somegroup"
}

Additional context
Ideal implementation

const morphed = morphism(customerSchema, customer.data)

Current result, with unresolved promise

{
"firstname": "somefirstname",
"lastname": "somelastname",
"email": "mail@mail.com",
"group": "Promise { <pending> }"
}

Ideal result, with resolved promise

{
"firstname": "somefirstname",
"lastname": "somelastname",
"email": "mail@mail.com",
"group": "somegroup"
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions