Feature Request: Support promises
roschaefer opened this issue · comments
Hey folks,
we would like to use Rosie.js to setup a database in a nodejs environment. The API of our database connection returns promises and so do our factories now. I have seen in the code that Rosie.js never waits for promises.
Would you accept a breaking change and return a promises for instance.build
and instance.buildList
?
Or would you accept a feature like this: Factory.defineAsync('model')
which would then a factory that returns a promise for the two methods above?
We work around this issue by never having more than one after
callback and never using buildList
.
I would be up to contribute a feature as mentioned above and would like to get an update about the status of this library.
You can use multiple after
callbacks as long as you await
the value from the previous one. Similarly, you can await the result of buildList
with Promise.all
. Here's an example on codesandbox.io (code pasted below for posterity).
import "./styles.css";
import { Factory } from "rosie";
const AsyncPerson = new Factory()
.attr("name")
.after(async p => {
const person = await p;
console.log("value in first 'after'", person);
return { ...person, addedInFirstAfter: true };
})
.after(async p => {
const person = await p;
console.log("value in second 'after'", person);
return { ...person, addedInSecondAfter: true };
});
(async () => {
// build one at a time
console.log(await AsyncPerson.build({ name: "Brian" }));
// build many at a time
console.log(await Promise.all(AsyncPerson.buildList(3, { name: "Test" })));
})();
@roschaefer, @eventualbuddha - I've made a PR for lazy async support #135
@jonstorer I'm not working on the referenced project but I will send a link to the contributors. I'm glad to see there is activity here @jonstorer thank you