Skip to content

Commit

Permalink
feat: newPromiseUntilTired
Browse files Browse the repository at this point in the history
BREAKING CHANGE: First Release
  • Loading branch information
ReiiYuki committed Mar 30, 2023
1 parent 3b5c9ae commit 69afe45
Show file tree
Hide file tree
Showing 21 changed files with 6,540 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["standard"],
"rules": {
"no-console": "off",
"semi": ["error", "never"]
}
}
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Release
on:
push:
branches:
- master

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*'
- name: Install dependencies
run: yarn
- name: Build
run: yarn build
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
logs
*.log
npm-debug.log*
.DS_Store

coverage
node_modules
build
.env.local
.env.development.local
.env.test.local
.env.production.local

*.cache
.env
5 changes: 5 additions & 0 deletions .huskyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hooks": {
"pre-commit": "lint-staged"
}
}
4 changes: 4 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.{ts,js}": ["prettier --write", "eslint --fix", "git add"],
"*.svg": ["svgo", "git add"]
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/fermium
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"bracketSpacing": true,
"jsxBracketSameLine": false,
"printWidth": 100,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": true,
"arrowParens": "avoid"
}
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-prefix ""
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Wongnai
Copyright (c) 2023 LINE MAN Wongnai

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Promise until tired

[![semantic-release](https://img.shields.io/badge/semantic-release-e10079.svg?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
![ts](https://badgen.net/badge/Built%20With/TypeScript/blue) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Too long unresolved promise is tired and I gonna give up.

This is the promise with timeout.

## Installation

```
yarn add promise-until-tired
```

### Usage

```ts
import newPromiseUntilTired, { TiredFromWaitingPromiseResolveTooLongError } from 'promise-until-tired'

try {
const value = await newPromiseUntilTired((resolve, reject) => {
// logic
}, timeout)
} catch (error) {
if (error instanceof TiredFromWaitingPromiseResolveTooLongError) {
// handle timeout logic
} else {
// handle other reject logic
}
}
```
26 changes: 26 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
moduleFileExtensions: ['json', 'ts', 'js'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
testRegex: '((\\.|/)(test|spec))\\.ts$',
transformIgnorePatterns: ['/node_modules/', '/dist/'],
testEnvironment: 'jsdom',
moduleDirectories: ['node_modules', 'src'],
coverageReporters: ['cobertura', 'lcov', 'json', 'text'],
coverageThreshold: {
global: {
statements: 80,
branches: 80,
functions: 80,
lines: 80,
},
},
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.d.ts',
'!src/**/types.ts',
'!src/**/constants.ts',
'!src/types/index.ts',
]
}
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "promise-until-tired",
"version": "0.0.0",
"types": "build",
"deploy": "build",
"entry": "src/index.ts",
"main": "build/index.js",
"repository": {
"type": "git",
"url": "https://github.com/wongnai/promise-until-tired.git"
},
"homepage": "https://github.com/wongnai/promise-until-tired#readme",
"license": "MIT",
"scripts": {
"build": "rollup -c",
"lint": "eslint --ext .ts,.js --quiet",
"test": "jest --coverage"
},
"devDependencies": {
"@rollup/plugin-commonjs": "19.0.0",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "13.0.0",
"@rollup/plugin-typescript": "8.2.1",
"@types/jest": "27.0.2",
"@types/node": "15.12.1",
"@types/qs": "6.9.7",
"eslint": "7.28.0",
"eslint-config-standard": "16.0.3",
"husky": "6.0.0",
"jest": "27.3.1",
"lint-staged": "11.0.0",
"prettier": "2.3.1",
"rollup": "2.51.0",
"rollup-plugin-cleaner": "1.0.0",
"rollup-plugin-includepaths": "0.2.4",
"rollup-plugin-visualizer": "5.5.0",
"semantic-release": "18.0.0",
"ts-jest": "27.0.7",
"typescript": "4.4.4"
}
}
33 changes: 33 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path'

import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import visualizer from 'rollup-plugin-visualizer'

import config from './package.json'

export default [{
input: config.entry,
output: {
format: 'cjs',
sourcemap: true,
dir: config.deploy,
},
plugins: [
resolve({
browser: true,
}),
commonjs(),
typescript({
declaration: true,
declarationDir: config.deploy,
exclude: 'src/**/*.test.ts',
}),
json(),
visualizer({
filename: path.resolve(config.deploy, 'stat.html'),
}),
],
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import TiredFromWaitingPromiseResolveTooLongError from '.'

describe('TiredFromWaitingPromiseResolveTooLongError', () => {
describe('message', () => {
it('should return message to inform that we give up already about the response from promise which including the limit of time to wait', () => {
expect(new TiredFromWaitingPromiseResolveTooLongError(10000).message).toBe('I have waited this promise resolving for 10 seconds, so I am tired and forgive to wait for this.')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MS_IN_SEC } from "variables"

class TiredFromWaitingPromiseResolveTooLongError extends Error {
constructor(timeout: number) {
super(`I have waited this promise resolving for ${timeout / MS_IN_SEC} seconds, so I am tired and forgive to wait for this.`)
}
}

export default TiredFromWaitingPromiseResolveTooLongError
46 changes: 46 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import TiredFromWaitingPromiseResolveTooLongError from "errors/TiredFromWaitingPromiseResolveTooLongError"
import newPromiseUntilTired from "."

describe('newPromiseUntilTired', () => {
describe('never resolve and timeout', () => {
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => {
await expect(newPromiseUntilTired<never>(() => {}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError)
})
})

describe('resolve after timeout', () => {
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => {
await expect(newPromiseUntilTired<void>((resolve) => {
setTimeout(resolve, 1000)
}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError)
})
})

describe('reject after timeout', () => {
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => {
await expect(newPromiseUntilTired<void>((_, reject) => {
setTimeout(reject, 1000)
}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError)
})
})

describe('resolve before timeout', () => {
it('should resolve the value correctly', async () => {
const expectedResolvedValue = 'resolved-value'

await expect(newPromiseUntilTired<string>((resolve) => {
setTimeout(() => resolve(expectedResolvedValue), 500)
}, 1000)).resolves.toBe(expectedResolvedValue)
})
})

describe('reject before timeout', () => {
it('should reject the value correctly', async () => {
const expectedRejectedValue = 'rejected-value'

await expect(newPromiseUntilTired<string>((_, reject) => {
setTimeout(() => reject(expectedRejectedValue), 500)
}, 1000)).rejects.toBe(expectedRejectedValue)
})
})
})
32 changes: 32 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import TimeoutError from "errors/TiredFromWaitingPromiseResolveTooLongError"
import { PromiseExecutor } from "types"

function newPromiseUntilTired<T>(executor: PromiseExecutor<T>, timeout: number) {
let isResolved = false

return new Promise<T>((resolve, reject) => {
setTimeout(() => {
if (!isResolved) {
reject(new TimeoutError(timeout))
}
}, timeout)

const resolveWithMarked = (resolvedValue: T) => {
isResolved = true

resolve(resolvedValue)
}

const rejectWithMarked = (rejectedValue: any) => {
isResolved = true

reject(rejectedValue)
}

executor(resolveWithMarked, rejectWithMarked)
})
}

export const TiredFromWaitingPromiseResolveTooLongError = TimeoutError

export default newPromiseUntilTired
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type ResolverFunction<T> = (resolvedValue: T) => void

export type PromiseExecutor<T> = (resolve: ResolverFunction<T>, reject: ResolverFunction<any>) => void
1 change: 1 addition & 0 deletions src/variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MS_IN_SEC = 1000
29 changes: 29 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es2019",
"module": "esnext",
"strict": true,
"baseUrl": "./src",
"importHelpers": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"types": [
"jest",
"node"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": false,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true
},
"include": [
"src/**/*.ts",
],
"exclude": [
"node_modules"
]
}
Loading

0 comments on commit 69afe45

Please sign in to comment.