• Home
Name Date Size #Lines LOC

..--

index.d.tsD12-May-20241.1 KiB408

index.jsD12-May-2024211 106

licenseD12-May-20241.1 KiB105

package.jsonD12-May-20241.7 KiB7574

readme.mdD12-May-20241.3 KiB5934

readme.md

1# p-try [![Build Status](https://travis-ci.org/sindresorhus/p-try.svg?branch=master)](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