Comparing version 0.0.0 to 1.0.0
24
index.js
function attempts (fn, args) { | ||
return args.reduce((prev, curr) => { | ||
return prev.then(resolved => resolved, rejected => { | ||
return args.reduce(function (prev, curr) { | ||
return prev.then(function (resolved) { return resolved; }, function (rejected) { | ||
try { | ||
const ret = fn(...[].concat(curr)) | ||
return ret ? Promise.resolve(ret) : Promise.reject(ret) | ||
var ret = fn.apply(void 0, [].concat(curr)) | ||
return ret ? Promise.resolve(ret) : Promise.reject() | ||
} catch (e) { | ||
return Promise.reject(e) | ||
return Promise.reject() | ||
} | ||
@@ -15,6 +15,6 @@ }) | ||
function attemptsSync (fn, args) { | ||
return args.reduce((prev, curr) => { | ||
if (prev) return prev | ||
return args.reduce(function (prev, curr) { | ||
if (prev) { return prev } | ||
try { | ||
const ret = fn(...[].concat(curr)) | ||
var ret = fn.apply(void 0, [].concat(curr)) | ||
return ret | ||
@@ -25,3 +25,7 @@ } catch (e) {} | ||
module.exports = attempts | ||
module.exports.sync = attemptsSync | ||
module.exports = { | ||
default: attempts, | ||
async: attempts, | ||
sync: attemptsSync | ||
} | ||
{ | ||
"name": "attempts", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Try func with different args.", | ||
"author": "Amio <amio.cn@gmail.com>", | ||
"reposotiry": "amio/attempts", | ||
"repository": "amio/attempts", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"build": "buble index.src.js > index.js", | ||
"pretest": "npm run build", | ||
"test": "tape tests.js" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.14.0", | ||
"tape": "^4.6.0" | ||
}, | ||
"keywords": [ | ||
@@ -13,0 +19,0 @@ "attempt", |
@@ -5,20 +5,29 @@ # attempts [![npm-version][npm-badge]][npm-link] | ||
## Methods | ||
- `attempts.sync(fn: function, args: array)` | ||
Try `fn` with `args` one by one, until find a truthy return value. | ||
- `attempts.async(fn: function, args: array)` returns a `Promise` | ||
Try `fn` with `args` one by one, until find a truthy return value or | ||
resolved Promise. | ||
Exceptions within `fn` will be catched & treated as falsy return. | ||
## Example | ||
Get availiable config: | ||
```javascript | ||
const fs = require('fs') | ||
const attempts = require('attempts') | ||
const files = [ | ||
'config.json', | ||
'config.default.json' | ||
const getConfig = filename => require(filename) | ||
const possibleConfigs = [ | ||
'./config.json', | ||
'./config.default.json' | ||
] | ||
function getConfig (filename) { | ||
return require(filename) | ||
} | ||
attempts(getConfig, files).then(config => { | ||
console.log('Found', config) | ||
}) | ||
const config = attempts.sync(getConfig, possibleConfigs) | ||
``` | ||
@@ -25,0 +34,0 @@ |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4975
7
68
1
41
2