-
Notifications
You must be signed in to change notification settings - Fork 21
Async morphism #106
Copy link
Copy link
Open
Labels
Description
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"
}Reactions are currently unavailable