New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

attempts

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

attempts - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

.npmignore

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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc