Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
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
|
---|
18 | const 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 |
|
---|
37 | 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.
|
---|
38 |
|
---|
39 | 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.
|
---|
40 |
|
---|
41 | #### fn
|
---|
42 |
|
---|
43 | The function to run to start the promise chain.
|
---|
44 |
|
---|
45 | #### arguments
|
---|
46 |
|
---|
47 | Arguments 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 |
|
---|
58 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.