retry-promise-func
Advanced tools
Comparing version 1.0.0 to 1.0.1
define(['exports'], (function (exports) { 'use strict'; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
exports.retry = retry; | ||
exports.retry = retry; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=index.amd.js.map |
var RetryPromiseFunc = (function (exports) { | ||
'use strict'; | ||
'use strict'; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
exports.retry = retry; | ||
exports.retry = retry; | ||
return exports; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
return exports; | ||
})({}); | ||
//# sourceMappingURL=index.iife.js.map |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** | ||
@@ -4,0 +6,0 @@ * 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.RetryPromiseFunc = {})); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.RetryPromiseFunc = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
/** | ||
* 尝试多次调用一个函数,如果失败则重试,直到成功或超过重试次数 | ||
* | ||
* @param fn 需要调用的函数,它应该返回一个Promise | ||
* @param params 调用函数时使用的参数 | ||
* @param times 最大重试次数,默认为40次 | ||
* @param delay 每次重试之间的延迟时间(毫秒),默认为1600ms | ||
* | ||
* @returns 当函数调用成功时解决Promise,否则拒绝Promise | ||
*/ | ||
const retry = async (fn, params = {}, times = 40, delay = 1600) => { | ||
return new Promise((resolve, reject) => { | ||
const tFn = function () { | ||
fn(params) | ||
.then(resolve) | ||
.catch((e) => { | ||
if (times-- > 0) { | ||
console.log(`还有${times}次机会`); | ||
setTimeout(tFn, delay); | ||
} | ||
else { | ||
reject(e); | ||
} | ||
}); | ||
}; | ||
return tFn(); | ||
}); | ||
}; | ||
exports.retry = retry; | ||
exports.retry = retry; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "retry-promise-func", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "retry-promise-func是一个用于在失败时重试Promise调用的工具。", | ||
"main": "./lib", | ||
"main": "lib/index.js", | ||
"module": "lib/index.esm.js", | ||
"jsnext:main": "lib/index.esm.js", | ||
"browser": "lib/index.umd.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "rollup -c", | ||
"clean": "rimraf ./lib", | ||
"clean": "rimraf lib", | ||
"prepare": "npm run clean && npm run build" | ||
}, | ||
"type": "module", | ||
"author": "Chain <caohuiboss@qq.com>", | ||
"author": "China <caohuiboss@qq.com>", | ||
"license": "ISC", | ||
@@ -28,14 +35,16 @@ "keywords": [ | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"rimraf": "^4.1.2", | ||
"rollup": "^3.15.0", | ||
"rollup": "^2.62.1", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-dts": "^5.2.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"typescript": "^4.9.5" | ||
"rollup-plugin-typescript2": "^0.34.1", | ||
"typescript": "^4.5.5" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^11.0.0", | ||
"@rollup/plugin-typescript": "^8.0.0", | ||
"tslib": "^2.5.0" | ||
} | ||
} |
@@ -7,5 +7,5 @@ # retry-promise-func | ||
使用 npm 安装: npm install retry-promise-func | ||
> 使用 npm 安装: npm install retry-promise-func | ||
使用 yarn 安装: yarn add retry-promise-func | ||
> 使用 yarn 安装: yarn add retry-promise-func | ||
@@ -17,7 +17,27 @@ ## 使用 | ||
```typescript | ||
import { retry } from "retry-promise"; | ||
import { retry } from "retry-promise-func"; | ||
retry(someFunction, someParams, 10, 2000) | ||
.then((result) => console.log(result)) | ||
.catch((error) => console.error(error)); | ||
const someFunction = async (someParams) => { | ||
return new Promise((resolve, reject) => { | ||
console.log(someParams); | ||
setTimeout(() => { | ||
if (Math.random() > 0.5) { | ||
resolve("Success"); | ||
} else { | ||
reject("Failure"); | ||
} | ||
}, 1000); | ||
}); | ||
}; | ||
const main = async () => { | ||
try { | ||
const result = await retry(someFunction, { a: 1 }, 10, 2000); | ||
console.log(result); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
main(); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42
9566
11
13
178
+ Added@rollup/plugin-commonjs@24.1.0(transitive)
+ Added@rollup/pluginutils@4.2.1(transitive)
+ Addedcommondir@1.0.1(transitive)
+ Addedfind-cache-dir@3.3.2(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedfs-extra@10.1.0(transitive)
+ Addedglob@8.1.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-reference@1.2.1(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedmagic-string@0.27.0(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpkg-dir@4.2.0(transitive)
+ Addedrollup-plugin-typescript2@0.34.1(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedtslib@2.7.0(transitive)
+ Addeduniversalify@2.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)
Updatedrollup@^2.62.1
Updatedtypescript@^4.5.5