-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ddf10d6
Showing
17 changed files
with
3,636 additions
and
0 deletions.
There are no files selected for viewing
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,12 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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,8 @@ | ||
coverage/ | ||
node_modules/ | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.nyc_output | ||
lib | ||
lib_test |
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,16 @@ | ||
# gitignore | ||
|
||
coverage/ | ||
node_modules/ | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.nyc_output | ||
lib | ||
lib_test | ||
|
||
# npmignore | ||
|
||
src/ | ||
__tests__/ | ||
.vscode/ |
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,7 @@ | ||
language: node_js | ||
node_js: | ||
- 'node' # use latest stable nodejs version | ||
script: | ||
- npm run coverage # jest test with coverage flag does coverage too | ||
after_script: | ||
- 'cat coverage/lcov.info | ./node_modules/.bin/coveralls' # sends the coverage report to coveralls |
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,32 @@ | ||
{ | ||
// Use IntelliSense to learn about possible Node.js debug attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug file", | ||
"program": "${workspaceRoot}/lib/${fileBasenameNoExtension}.js", | ||
"cwd": "${workspaceRoot}", | ||
"sourceMaps": true, | ||
"smartStep": true, | ||
"preLaunchTask": "build", | ||
"outFiles": [ | ||
"${workspaceRoot}/lib/*.js" | ||
] | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug test", | ||
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js", | ||
"args": [ | ||
"--findRelatedTests", | ||
"${relativeFile}" | ||
], | ||
"cwd": "${workspaceRoot}" | ||
} | ||
] | ||
} |
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 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib/" | ||
} |
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,59 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"taskName": "Run current file", | ||
"command": "ts-node ${relativeFile}", | ||
"type": "shell", | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "clean", | ||
"script": "clean", | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "build", | ||
"script": "build", | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
}, | ||
"problemMatcher":[] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "format", | ||
"script": "format", | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "coverage", | ||
"script": "coverage", | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "test", | ||
"script": "test", | ||
"group": { | ||
"kind": "test", | ||
"isDefault": true | ||
}, | ||
"problemMatcher": [] | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "lint", | ||
"script": "lint", | ||
"problemMatcher": [ | ||
"$eslint-stylish" | ||
] | ||
} | ||
] | ||
} |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,39 @@ | ||
[![Build Status](https://travis-ci.org/kuyoonjo/ctry.svg?branch=master)](https://travis-ci.org/kuyoonjo/ctry.svg?branch=master) | ||
[![Coverage Status](https://coveralls.io/repos/github/kuyoonjo/ctry/badge.svg?branch=master)](https://coveralls.io/github/kuyoonjo/ctry?branch=master) | ||
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) | ||
|
||
# Intro | ||
|
||
`ctry` tries to resolve a promise with `timeout` and `chance` configurations. | ||
|
||
```ts | ||
/** | ||
* ctry | ||
* @param ctryFunc a function returns a promise | ||
* @param timeout timeout in ms | ||
* @param tries number of changes | ||
*/ | ||
function ctry<T>( | ||
ctryFunc: CtryFunction<T>, | ||
timeout: number = 5000, | ||
tries: number = 1, | ||
): Promise<T>; | ||
``` | ||
|
||
> Q: When shoul you use it? | ||
> A: Trying to do sth. with timeout, and/or give it more than one chance. | ||
eg. Trying validate a web server whether it is reachable. The codes use `HEAD` method to do a request, and try 3 times. | ||
|
||
```ts | ||
import axios from 'axios'; | ||
import { ctry } from 'ctry'; | ||
|
||
(async () => { | ||
try { | ||
await ctry(() => axios.head('https://example.com'), 5000, 3); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
})(); | ||
``` |
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,55 @@ | ||
import { ctry, ERR_TIMEOUT } from '../src/ctry'; | ||
|
||
test('Should resolve', async () => { | ||
const fn = () => Promise.resolve(1); | ||
const res = await ctry(_ => fn()); | ||
expect(res).toBe(1); | ||
}); | ||
|
||
test('Should reject', async () => { | ||
const fn = () => Promise.reject(1); | ||
try { | ||
await ctry(_ => fn(), 1000, 1); | ||
} catch (e) { | ||
expect(e).toBe(1); | ||
} | ||
}); | ||
|
||
test('Should reject with timeout', async () => { | ||
const fn = () => | ||
new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(1); | ||
}, 1000); | ||
}); | ||
try { | ||
await ctry(_ => fn(), 100, 1); | ||
} catch (e) { | ||
expect(e.message).toBe(ERR_TIMEOUT); | ||
} | ||
}); | ||
|
||
test('Should resolve with 3 tries', async () => { | ||
const p = new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(1); | ||
}, 1000); | ||
}); | ||
const fn = () => p; | ||
const res = await ctry(_ => fn(), 400, 3); | ||
expect(res).toBe(1); | ||
}); | ||
|
||
test('Should reject with 2 tries', async () => { | ||
const p = new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(1); | ||
}, 1000); | ||
}); | ||
const fn = () => p; | ||
try { | ||
await ctry(_ => fn(), 400, 2); | ||
} catch (e) { | ||
expect(e.message).toBe(ERR_TIMEOUT); | ||
} | ||
}); |
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,6 @@ | ||
import * as index from '../src/index'; | ||
|
||
test('Should export', () => { | ||
expect(index.ctry).toBeTruthy(); | ||
expect(index.ERR_TIMEOUT).toBeTruthy(); | ||
}); |
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,55 @@ | ||
{ | ||
"name": "ctry", | ||
"version": "1.0.0", | ||
"description": "ctry tries to resolve a promise with timeout and chance configurations.", | ||
"license": "MIT", | ||
"repository": "https://github.com/kuyoonjo/ctry.git", | ||
"author": { | ||
"name": "Yu Chen", | ||
"email": "yu@chen.news", | ||
"url": "https://yu.chen.news" | ||
}, | ||
"keywords": [""], | ||
"files": ["lib"], | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"clean": "rimraf lib && rimraf coverage", | ||
"format": | ||
"prettier --write \"{src,__tests__}/**/*.ts\" --single-quote --trailing-comma es5", | ||
"lint": "tslint --force --format verbose \"src/**/*.ts\"", | ||
"prepublishOnly": "npm run build", | ||
"prebuild": | ||
"npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version", | ||
"build": "tsc --pretty", | ||
"test": "jest", | ||
"coverage": "jest --coverage", | ||
"watch": "npm run build -- --watch", | ||
"watch:test": "jest --watch" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/jest": "^23.3.3", | ||
"@types/node": "^10.11.4", | ||
"coveralls": "^3.0.2", | ||
"jest": "^23.6.0", | ||
"prettier": "^1.14.3", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^23.10.3", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.11.0", | ||
"tslint-config-prettier": "^1.15.0", | ||
"typescript": "^3.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"jest": { | ||
"transform": { | ||
".(ts)": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|js)$", | ||
"moduleFileExtensions": ["ts", "js"], | ||
"testEnvironment": "node" | ||
} | ||
} |
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 @@ | ||
/* | ||
* @Author: Yu Chen | ||
* @Date: 2018-11-12 17:44:56 | ||
* @Last Modified by: Yu Chen | ||
* @Last Modified time: 2018-11-12 21:17:40 | ||
*/ | ||
|
||
export const ERR_TIMEOUT = 'ERR_TIMEOUT'; | ||
|
||
/** | ||
* ctry | ||
* @param ctryFunc a function returns a promise | ||
* @param timeout timeout in ms | ||
* @param tries number of changes | ||
*/ | ||
export function ctry<T>( | ||
ctryFunc: CtryFunction<T>, | ||
timeout: number = 5000, | ||
tries: number = 1 | ||
): Promise<T> { | ||
const tf = () => | ||
new Promise<never>((_, reject) => { | ||
const id = setTimeout(() => { | ||
clearTimeout(id); | ||
reject(new Error(ERR_TIMEOUT)); | ||
}, timeout); | ||
}); | ||
|
||
const tryit = async (t = 1): Promise<T> => { | ||
try { | ||
const res = await Promise.race([ctryFunc(t), tf()]); | ||
return res; | ||
} catch (e) { | ||
if (ERR_TIMEOUT === e.message && t < tries) { | ||
return tryit(t + 1); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
return tryit(); | ||
} | ||
|
||
export type CtryFunction<T> = (challenge: number) => Promise<T>; |
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 './ctry'; |
Oops, something went wrong.