Using Holster for building i.haza.website was very different than working on RSStream as it needed to be much more responsive to Holster data changes. Looking back at the code up until this point, it was full of await new Promise(resolve => { }) wrappers so that state could update in the correct order.
The latest Holster release removes all of this from application code by providing a Thenables interface onto terminal API calls. That sounds weird but it does exactly what you need, which is when you would otherwise provide a callback to the Holster API you can just await the call instead. Internally Holster knows when a callback isn't provided and can extend the response to include a then() function, if you were chaining the request then the extra function is ignored, otherwise it provides your data.
This works for get and next in the API, put requests can also be awaited but they now internally return a promise as they only ever returned an error or null anyway. It allows for simplifying get requests like:
// From wrapped calls:
const existing = await new Promise(resolve =>
user.get("mediaRootId", resolve),
)
// To just:
const existing = await user.get("mediaRootId")
And previously put requests where you waited for the result were:
async function bumpProjectsVersion(user) {
const now = Date.now()
return new Promise((resolve, reject) =>
user
.get("projectsVersion")
.put(now, err => (err ? reject(new Error(err)) : resolve(now))),
)
}
Are now:
async function bumpProjectsVersion(user) {
const now = Date.now()
await user.get("projectsVersion").put(now)
return now
}
It also makes it easier to work with try and catch blocks, but it's also nice that the old callback style isn't effected by these changes when you want to use it.
Holster 2.3.0
awaitsupport.Using Holster for building i.haza.website was very different than working on RSStream as it needed to be much more responsive to Holster data changes. Looking back at the code up until this point, it was full of
await new Promise(resolve => { })wrappers so that state could update in the correct order.The latest Holster release removes all of this from application code by providing a Thenables interface onto terminal API calls. That sounds weird but it does exactly what you need, which is when you would otherwise provide a callback to the Holster API you can just
awaitthe call instead. Internally Holster knows when a callback isn't provided and can extend the response to include athen()function, if you were chaining the request then the extra function is ignored, otherwise it provides your data.This works for
getandnextin the API,putrequests can also be awaited but they now internally return a promise as they only ever returned an error ornullanyway. It allows for simplifying get requests like:// From wrapped calls: const existing = await new Promise(resolve => user.get("mediaRootId", resolve), ) // To just: const existing = await user.get("mediaRootId")And previously put requests where you waited for the result were:async function bumpProjectsVersion(user) { const now = Date.now() return new Promise((resolve, reject) => user .get("projectsVersion") .put(now, err => (err ? reject(new Error(err)) : resolve(now))), ) }Are now:async function bumpProjectsVersion(user) { const now = Date.now() await user.get("projectsVersion").put(now) return now }It also makes it easier to work with try and catch blocks, but it's also nice that the old callback style isn't effected by these changes when you want to use it.