find
is a function that returns a promise that resolves to a value, but also has data on the promise object itself. A practical use case is when find()
returns a dbQueryObject that can be used elsewhere, whereas await find()
resolves with databaseResults. eg:
function find () {
var results = new Promise(/* resolves with databaseResults */)
Object.assign(results, new dbQueryClass)
return results
}
I can wrap find
with a function to provide helpers like this,
function findPage (page) {
return find().skip(page*20).limit(20)
}
and use it like findPage().select('_id')
to get the query object or await findPage().select('_id')
to get the resolved value (returns something similar to find()
).
However, if I wrap find
with a promise like this,
async function findSomething() {
var someArgs = await promise1()
if (someArgs) return find(someArgs)
return find()
}
How do I get the value of find()
itself outside of findSomething
? I need promise1
to resolve, but I need find()
to not resolve. find()
is a plain object AND a thenable, depending on if you resolve it or not, but resolving findSomething()
will automatically resolve the returned value. How do I return it such that it doesn't resolve?
My constraint is that I cannot modify the library that provides find()
to not return a thenable object.