![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/vreg/node_modules/p-try/ |
# p-try [](https://travis-ci.org/sindresorhus/p-try) > Start a promise chain [How is it useful?](http://cryto.net/~joepie91/blog/2016/05/11/what-is-promise-try-and-why-does-it-matter/) ## Install ``` $ npm install p-try ``` ## Usage ```js const pTry = require('p-try'); (async () => { try { const value = await pTry(() => { return synchronousFunctionThatMightThrow(); }); console.log(value); } catch (error) { console.error(error); } })(); ``` ## API ### pTry(fn, ...arguments) 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 - [p-finally](https://github.com/sindresorhus/p-finally) - `Promise#finally()` ponyfill - Invoked when the promise is settled regardless of outcome - [More…](https://github.com/sindresorhus/promise-fun) ## License MIT © [Sindre Sorhus](https://sindresorhus.com)