Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-context

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-context

Context type that carries deadlines and cancelation signals.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

Context Build Status

Context type that carries deadlines, cancelation signals, and other request-scoped values.

Influenced by Go's context.

Usage

var context = require('node-context');

function work(ctx, name) {
  ctx.once('done', function() {
    console.log(name + ' done %d (deadline %d)', new Date() - ctx.time, ctx.deadline);
  });
}

function main() {
  var ctx = context({ timeout: 1000 });
  ctx.time = new Date();

  ctx.once('done', function() {
    console.log('main done %d (deadline %d)', new Date() - ctx.time, ctx.deadline);
  });

  work(ctx.create({ timeout: 500 }), 'work1');
  work(ctx.create(), 'work2');
}

main();

Output

work1 done 524 (deadline 1424550908411)
main done 1002 (deadline 1424550908908)
work2 done 1005 (deadline 1424550908908)

Documentation

### context.Context([options])

Initialize a new context.

Options

  • deadline (Number, optional): time when request expires (milliseconds since Unix epoch)
  • timeout (Number, optional): duration in milliseconds until request expires
  • parent (Context, optional): parent context; inherits deadlines, cancelation signals, and properties
### Event: 'done'

Emitted when the request exceeds it's deadline or is canceled.

This is only emitted once.

### ctx.cancel()

Cancel/finish request immediately.

This should always be called when the request is finished and is safe to call multiple times.

### ctx.create([options])

Create and return a child context.

This accepts the same options as the context constructor and automatically sets the parent option.

### ctx.deadline

Number of milliseconds since Unix epoch.

License

This work is licensed under the MIT License (see the LICENSE file).

Keywords

FAQs

Package last updated on 21 Feb 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc