@kanziw/promise
v0.1.1
Published
A collection of utility libraries about promise
Downloads
6
Readme
@kanziw/promise
defer
Handle promise resolve/reject manually.
import { defer } from '@kanziw/promise'
async function rollbackableInsert() {
const commitOrRollback = defer<void>()
const insertedId = defer<string>()
const txPromise = db.withTransaction(async conn => {
const { insertId } = await conn.execute(qb.insertInto('table').values({}))
.then(insertedId.resolve)
.catch(err => {
insertedId.reject(err)
throw err
});
await commitOrRollback.promise
})
const commit = async () => {
commitOrRollback.resolve()
return txPromise
};
return {
insertId: await insertedId.promise,
commit,
rollback: deferWaitTransaction.reject,
}
}
const { insertId, commit, rollback } = await rollbackableInsert()
try {
// some business logic
await commit()
} catch (err) {
await rollback()
throw err
}