Skip to content

Commit 6cb9453

Browse files
committed
feat: added option
added catchPromisesReject options defaulting to true updated docs
1 parent 8a88665 commit 6cb9453

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919

2020
## About The Project
2121

22-
This packages aim is to simplify programmatically generate flags to use in Unix-style command by declaring them with an object literal and turning them into an array or a string of usable flags.
22+
The purpose of this package is gracefully exiting Node.js processes the "good way" (opinated clearly), while also providing some minir extendability!
2323

24-
Here's why:
25-
26-
- You exec/spawn commands and need to provide options
27-
- You want to dynamically add flags without bunch of string concats
28-
- you don't want to have an headache refactoring when needed
24+
This package also takes an array of callbacks that will get executed serially to allow closing user defined resource, eg: a database connection.
2925

3026
<!-- GETTING STARTED -->
3127

@@ -43,7 +39,19 @@ npm i @scdev/fine
4339

4440
```js
4541
const fine = require("@scdev/fine");
46-
fine({ timeout: 2000 }, [() => db.disconnect()]);
42+
fine(
43+
{
44+
timeout: 2000,
45+
catchPromisesReject: true,
46+
},
47+
[
48+
redis.disconnect,
49+
() => {
50+
// custom logic
51+
return db.disconnect()
52+
}
53+
]
54+
);
4755
```
4856

4957
### Arguments

index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ module.exports = (opts = {}, closeFunctions = []) => {
1010
const options = Object.assign(
1111
{
1212
timeout: 2000,
13+
catchPromisesReject: true,
1314
},
1415
opts
1516
);
1617

17-
for (const sig of [
18-
"SIGINT",
19-
"SIGTERM",
20-
"uncaughtException",
21-
"unhandledRejection",
22-
]) {
23-
process.once(sig, () => {
24-
const code = sig.match("^SIG") ? 0 : 1;
25-
process.stdout.write(`[${sig}] exiting with code ${code}\n`);
18+
let events = ["SIGINT", "SIGTERM", "uncaughtException"];
19+
20+
if (options.catchPromisesReject) {
21+
events.push("unhandledRejection");
22+
}
23+
for (const event of events) {
24+
process.once(event, () => {
25+
const code = event.match("^SIG") ? 0 : 1;
26+
process.stdout.write(`[${event}] exiting with code ${code}\n`);
2627
if (Array.isArray(closeFunctions)) {
2728
for (const cb of closeFunctions) {
2829
try {

0 commit comments

Comments
 (0)