-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add promise package with defer (#18)
- Loading branch information
Showing
12 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src/ | ||
**/__tests__/ | ||
tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# @kanziw/promise | ||
|
||
[![npm version](https://img.shields.io/npm/v/@kanziw/promise)](https://www.npmjs.com/package/@kanziw/promise) | ||
[![license](https://img.shields.io/npm/l/@kanziw/promise)](https://www.npmjs.com/package/@kanziw/promise) | ||
[![npm downloads](https://img.shields.io/npm/dt/@kanziw/promise)](https://www.npmjs.com/package/@kanziw/promise) | ||
|
||
|
||
## defer | ||
|
||
Handle promise resolve/reject manually. | ||
|
||
```ts | ||
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@kanziw/promise", | ||
"version": "0.1.0", | ||
"description": "A collection of utility libraries about promise", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/kanziw/kanziwjs.git", | ||
"directory": "packages/promise" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"homepage": "https://github.com/kanziw/kanziwjs/tree/main/packages/promise#readme", | ||
"keywords": [ | ||
"promise", | ||
"kanziw", | ||
"defer" | ||
], | ||
"author": { | ||
"name": "Jiwoong Jung", | ||
"email": "kanziwoong@gmail.com" | ||
}, | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"packageManager": "yarn@3.2.0", | ||
"scripts": { | ||
"lint": "yarn workspace kanziwjs eslint packages/promise", | ||
"test": "yarn workspace kanziwjs test packages/promise", | ||
"clean": "rimraf lib", | ||
"build": "yarn clean && yarn tsc", | ||
"publish": "yarn build && yarn publish-if-not-published" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.5.0", | ||
"@types/node": "^17.0.25", | ||
"publish-if-not-published": "^3.1.2", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.6.3" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { setTimeout as delay } from 'timers/promises' | ||
|
||
import { defer } from '../defer' | ||
|
||
describe('defer', () => { | ||
it('success w/ resolve', async() => { | ||
const { deferWait, checkDeferWaitStatus } = deferTester() | ||
|
||
const beforeResolveResult = await checkDeferWaitStatus() | ||
expect(beforeResolveResult).toEqual('pending') | ||
|
||
deferWait.resolve() | ||
const afterResolveResult = await checkDeferWaitStatus() | ||
expect(afterResolveResult).toEqual('resolved') | ||
}) | ||
|
||
it('success w/ reject', async() => { | ||
const { deferWait, checkDeferWaitStatus } = deferTester() | ||
|
||
const beforeRejectResult = await checkDeferWaitStatus() | ||
expect(beforeRejectResult).toEqual('pending') | ||
|
||
deferWait.reject() | ||
const afterRejectResult = await checkDeferWaitStatus() | ||
expect(afterRejectResult).toEqual('rejected') | ||
}) | ||
}) | ||
|
||
function deferTester() { | ||
const deferWait = defer() | ||
const checkDeferWaitStatus = async() => ( | ||
Promise.race([ | ||
delay(10).then(() => 'pending'), | ||
deferWait.promise | ||
.then(() => 'resolved') | ||
.catch(() => 'rejected'), | ||
]) | ||
) | ||
|
||
return { | ||
deferWait, | ||
checkDeferWaitStatus, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
|
||
export const defer = <T=void>() => { | ||
const defer = { | ||
promise: Promise.resolve(undefined as unknown as T), | ||
resolve: (_value: T | PromiseLike<T>) => {}, | ||
reject: (_reason?: any) => {}, | ||
} | ||
defer.promise = new Promise((resolve, reject) => { | ||
defer.resolve = resolve | ||
defer.reject = reject | ||
}) | ||
|
||
return defer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './defer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib", | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters