-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
27 lines (25 loc) · 852 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const isPromise = require('is-promise');
const handle = p =>
p.catch(err => {
console.error(err);
process.exit(1);
});
/**
* <a id="main"></a>
* Catches a promise error, writes the stacktrace to stderr and exists
*
* [![](https://img.shields.io/npm/v/apr-main.svg?style=flat-square)](https://www.npmjs.com/package/apr-main) [![](https://img.shields.io/npm/l/apr-main.svg?style=flat-square)](https://www.npmjs.com/package/apr-main)
*
* @kind function
* @name main
* @param {Promise} input
* @returns {Promise}
*
* @example
* import main from 'apr-main';
*
* main(async () => 'hello') // writes nothing
* main(async () => undefined) // writes nothing
* main(async () => { throw new Error('uncaught error') }) // writes the stack trace to stderr and exists
*/
module.exports = p => (isPromise(p) ? handle(p) : handle(p()));