Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 9, 2025
0 parents commit 817a37d
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2025, Nordic Semiconductor ASA | nordicsemi.no
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@nrfcloud/wait-for-it`

<https://jsr.io/@nrfcloud/wait-for-it>
7 changes: 7 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@nrfcloud/wait-for-it",
"version": "1.0.5",
"license": "BSD-3-Clause",
"exports": "./waitForIt.ts"
}
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@nrfcloud/wait-for-it",
"version": "1.0.5",
"description": "Retries the given function using a backoff algorithm.",
"homepage": "https://github.com/nrfcloud/wait-for-it#readme",
"bugs": {
"url": "https://github.com/nrfcloud/wait-for-it/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nrfcloud/wait-for-it.git"
},
"author": "Nordic Semiconductor ASA | nordicsemi.no",
"license": "BSD-3-Clause",
"type": "module",
"main": "waitForIt.ts",
"scripts": {
"test": "npm test"
},
"dependencies": {
"backoff": "2.5.0"
},
"devDependencies": {
"@types/backoff": "2.5.5"
}
}
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "esnext",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "nodenext",
"moduleResolution": "nodenext",
"resolveJsonModule": true,
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"verbatimModuleSyntax": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"allowImportingTsExtensions": true
}
}
49 changes: 49 additions & 0 deletions waitForIt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { exponential } from "backoff";

/**
* @module
*
* This module contains the function that retries a given function.
*/

/**
* Retries the given function using a backoff algorithm.
* It times out after 27.75 sec by default (9 attempts)
*
* @example Wait for a tenant to be available
* ```ts
* import * as wait_for_it from "@nrfcloud/wait-for-it";
*
* const tenant = await wait_for_it<Tenant>(() =>
* repo.getByUUID(
* e.aggregateUUID,
* )
* );
* ```
*/
export const waitForIt = <A>(
fn: () => Promise<A>,
retryNumber: number = 9
): Promise<A> =>
new Promise((resolve, reject) => {
const b = exponential({
randomisationFactor: 0,
initialDelay: 250,
maxDelay: 5000,
});
let lastErr: Error;
b.failAfter(retryNumber);
b.on("ready", async () => {
try {
const res = await fn();
return resolve(res);
} catch (e) {
lastErr = e as Error;
}
b.backoff();
});
b.on("fail", () => {
reject(lastErr);
});
b.backoff();
});

0 comments on commit 817a37d

Please sign in to comment.