Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
node-context
Advanced tools
Context type that carries deadlines, cancelation signals, and other request-scoped values.
Context type that carries deadlines, cancelation signals, and other request-scoped values.
Influenced by Go's context.
Initialize a new context.
Options
Emitted when the request exceeds it's deadline or is canceled.
This is only emitted once.
### Event: 'finish'Emitted when the request is finished.
This is only emitted once.
### ctx.create([options])Create and return a child context.
This accepts the same options as the context constructor and automatically sets the parent
option.
Cancel request immediately.
This is safe to call multiple times.
### ctx.canceledTrue after canceled.
### ctx.deadlineTime in milliseconds when the request will be canceled if not finished.
### ctx.end()Finish the request, this must be called at the end of the request.
It is safe to call multiple times.
### ctx.finishedTrue after finished.
### ctx.values([object])Return context values.
If object
is provided then the context values will be merged into it.
var context = require('node-context');
function display(ctx, name) {
ctx.once('cancel', function() {
console.log('%s canceled after %d ms', name, new Date() - ctx.time);
});
ctx.once('finish', function() {
console.log('%s finished', name);
});
}
var parent = context();
parent.time = new Date();
var master = parent.create({ timeout: 1000 });
display(parent, 'parent');
display(master, 'master');
display(master.create({ timeout: 500 }), 'worker1');
display(master.create(), 'worker2');
process.on('beforeExit', function() {
parent.end();
});
Output
worker1 canceled after 524 ms
worker1 finished
master canceled after 1007 ms
worker2 canceled after 1007 ms
worker2 finished
master finished
parent finished
This work is licensed under the MIT License (see the LICENSE file).
FAQs
Context type that carries deadlines, cancelation signals, and other request-scoped values.
We found that node-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.