Comparing version 2.0.0 to 2.1.0
'use strict'; | ||
module.exports = (callback, ...args) => new Promise(resolve => { | ||
resolve(callback(...args)); | ||
const pTry = (fn, ...arguments_) => new Promise(resolve => { | ||
resolve(fn(...arguments_)); | ||
}); | ||
module.exports = pTry; | ||
module.exports.default = pTry; |
{ | ||
"name": "p-try", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "`Start a promise chain", | ||
@@ -16,6 +16,7 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -38,5 +39,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
"ava": "^1.3.1", | ||
"tsd-check": "^0.5.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -20,9 +20,12 @@ # p-try [![Build Status](https://travis-ci.org/sindresorhus/p-try.svg?branch=master)](https://travis-ci.org/sindresorhus/p-try) | ||
pTry(() => { | ||
return synchronousFunctionThatMightThrow(); | ||
}).then(value => { | ||
console.log(value); | ||
}).catch(error => { | ||
console.error(error); | ||
}); | ||
(async () => { | ||
try { | ||
const value = await pTry(() => { | ||
return synchronousFunctionThatMightThrow(); | ||
}); | ||
console.log(value); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
})(); | ||
``` | ||
@@ -33,9 +36,17 @@ | ||
### pTry(fn, ...args) | ||
### pTry(fn, ...arguments) | ||
Returns a `Promise` resolved with the value of calling `fn(...args)`. If the function throws an error, the returned `Promise` will be rejected with that error. | ||
Returns a `Promise` resolved with the value of calling `fn(...arguments)`. If the function throws an error, the returned `Promise` will be rejected with that error. | ||
Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions. | ||
#### fn | ||
The function to run to start the promise chain. | ||
#### arguments | ||
Arguments to pass to `fn`. | ||
## Related | ||
@@ -42,0 +53,0 @@ |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3960
5
30
59
0
3