| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| index.d.ts | D | 12-May-2024 | 1.1 KiB | 40 | 8 | |
| index.js | D | 12-May-2024 | 211 | 10 | 6 | |
| license | D | 12-May-2024 | 1.1 KiB | 10 | 5 | |
| package.json | D | 12-May-2024 | 1.7 KiB | 75 | 74 | |
| readme.md | D | 12-May-2024 | 1.3 KiB | 59 | 34 |
readme.md
1# p-try [](https://travis-ci.org/sindresorhus/p-try) 2 3> Start a promise chain 4 5[How is it useful?](http://cryto.net/~joepie91/blog/2016/05/11/what-is-promise-try-and-why-does-it-matter/) 6 7 8## Install 9 10``` 11$ npm install p-try 12``` 13 14 15## Usage 16 17```js 18const pTry = require('p-try'); 19 20(async () => { 21 try { 22 const value = await pTry(() => { 23 return synchronousFunctionThatMightThrow(); 24 }); 25 console.log(value); 26 } catch (error) { 27 console.error(error); 28 } 29})(); 30``` 31 32 33## API 34 35### pTry(fn, ...arguments) 36 37Returns 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. 38 39Support 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. 40 41#### fn 42 43The function to run to start the promise chain. 44 45#### arguments 46 47Arguments to pass to `fn`. 48 49 50## Related 51 52- [p-finally](https://github.com/sindresorhus/p-finally) - `Promise#finally()` ponyfill - Invoked when the promise is settled regardless of outcome 53- [More…](https://github.com/sindresorhus/promise-fun) 54 55 56## License 57 58MIT © [Sindre Sorhus](https://sindresorhus.com) 59