Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including when-connected with all npm packages installed. Try it out:

var whenConnected = require("when-connected")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

when-connected v0.0.4

call an async method with configurable timeout and when connected if reconnect is used

Build Status


perform a callback when "connected" or fire callback with timeout error if its taken too long.

the most important thing this does is provides you with the errors required to manage callback and stream disconnection in necessary ways.


this lib is generic and ensures that wrapped calls are only made when connected. these examples use multilevel a package used to interact with leveldb over the network but it is not the only reason this is useful.

var mulitlevel = require('multilevel');
var reconnect = require('reconnect');
var whenConnected = require('when-connected')

var client;
var recon = reconnect(function(stream){
  client = multilevel.client();

var get = whenConnected(function(key,cb){
var dumpDB = whenConnected(function(){
  return client.createReadStream();

// get a key.
  console.log('get called back');

var dump = dumpDB();
// output a db dump.
  console.log('db: ',data);


this exports one function.

whenConnected([the function to call when connected],[options])


  • reconnect
    • an EventEmitter that provides two events [connect],[disconnect] and has a [connected] property which will be used to understand the inital state of the connection.
    • if no reconnect is passed this will still work but about the only other useful thing it provides is timeout callbacks.
  • stream
    • required if the wrapped method returns a stream.
    • TODO duplex. if anyone needs it.
  • timeout
    • optional default -1 / no timeout
    • a maximum numbner of miliseconds to wait for...
    • any callback method to call back
    • or any stream method to get called and return a stream
    • the error object will have a [code] property that equals 'E_TIMEDOUT'
  • retries
    • optional default 0
    • the number of times the wrapped call should be retried if a 'disconnect' event is fired before the callback is complete.
    • this does not increase the timeout for the call.
    • if retires is exceded or not provided any disconnect while waiting for a call will callback with an E_DISCONNECT error


error objects returned by this lib all have a code proerty that is one of these values. they are either emitted as errors onn streams or passed as the error to a callback.

    • the time for callback to be called excedes specified value.
    • the time to get a connection to a method that returns a stream took to long to get a connection.
    • a disconnect event has been fired while a callback was pending.
    • a disconnect event was fired before the end event was triggered on a stream.
    • a method defined as returns streams did not return a stream. this is emitted on the proxy stream you have a handle too so bind error


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free