Say I have the following in thing.js
:
var db = require('my-database-module');
module.exports = class Thing() {
constructor(id, parentId, name) {
this.id = id;
this.parentId = parentId;
this.name = name;
}
static find(id) {
// NOTE: The following find() function returns a Promise.
return db.collection('things').find(id);
}
parent() {
return this.constructor.find(this.parentId);
}
}
Normally finding Things would be accomplished with the following:
var Thing = require('thing');
Thing.find(123).then(function(thing) {
// Do something with `thing`
});
You'll notice that I'm wanting to implement a parent/child hierarchy. I would like to add an ancestors
function that returns an array of ancestor Thing
objects for a given instance of Thing
:
module.exports = class Thing() {
// ...
ancestors() {
var a = []
// Can't figure this out...
return a;
}
}
Because the Thing#parent
function returns a Promise, I'm getting confused on how the ancestors
function should work. It would need to recursively lookup successive parents of the Thing
instance.
I've seen the Array.prototype.reduce
function can be used for chaining Promises, but I don't know the Promises to chain upfront since it needs to recursively look for the parent, grandparent, great grandparent, etc.
Any ideas on how to structure this function?