Skip to content

Commit

Permalink
docs: fixed example
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jul 9, 2020
1 parent 2bac756 commit fa9b4b5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@ function cancel() {
// send a message to the parent that we're ready to terminate
// (you could do `process.exit(0)` or `process.exit(1)` instead if desired
// but this is a bit of a cleaner approach for worker termination
parentPort.postMessage('cancelled');
if (parentPort) parentPort.postMessage('cancelled');
else process.exit(0);
}

parentPort.once('message', message => {
if (message === 'cancel') return cancel();
});
if (parentPort)
parentPort.once('message', message => {
if (message === 'cancel') return cancel();
});
```

If you'd like jobs to retry, simply wrap your usage of promises with [p-retry][].
Expand Down Expand Up @@ -282,16 +284,15 @@ const ms = require('ms');
await delay(ms('10s'));

// signal to parent that the job is done
parentPort.postMessage('done');

// you could also `process.exit(0);` when done
if (parentPort) parentPort.postMessage('done');
else process.exit(0);
})();
```


## Callbacks, Done, and Completion States

To close out the worker and signal that it is done, you can simply `parentPort.postMessage('done');` OR `process.exit(0)`.
To close out the worker and signal that it is done, you can simply `parentPort.postMessage('done');` and/or `process.exit(0)`.

While writing your jobs (which will run in [worker][workers] threads), you should do one of the following:

Expand Down

0 comments on commit fa9b4b5

Please sign in to comment.