flattenPromise
Overview
flattenPromise
is a utility that makes it easier to work with Promise<Dependency>
passed into your services.
For example, it simplifies the implementation of the TodosRepo
from the IndexedDB example.
ts
function createTodosRepo(idbPromise: Promise<IDBPDatabase>) {
const idb = flattenPromise(idbPromise);
return {
async create(todo: Todo): Promise<void> {
await (await idbPromise).add('todos', todo);
await idb.add('todos', todo);
},
// ...
};
}
It works by using a Proxy
to await the promise internally before calling any methods.