Name: js-handler/node_modules/restify/node_modules/backoff/examples/function_call.js 
1:
#!/usr/bin/env node
2:
 
3:
var backoff = require('../index.js'),
4:
    util = require('util'),
5:
    http = require('http');
6:
 
7:
var URL = 'http://www.iana.org/domains/example/';
8:
 
9:
function get(options, callback) {
10:
    http.get(options, function(res) {
11:
        res.setEncoding('utf8');
12:
        res.data = '';
13:
        res.on('data', function (chunk) {
14:
            res.data += chunk;
15:
        });
16:
        res.on('end', function() {
17:
            callback(null, res);
18:
        });
19:
        res.on('close', function(err) {
20:
            callback(err, res);
21:
        });
22:
    }).on('error', function(err) {
23:
        callback(err, null);
24:
    });
25:
}
26:
 
27:
 
28:
var call = backoff.call(get, URL, function(err, res) {
29:
    // Notice how the call is captured inside the closure.
30:
    console.log('Retries: ' + call.getResults().length);
31:
 
32:
    if (err) {
33:
        console.log('Error: ' + err.message);
34:
    } else {
35:
        console.log('Status: ' + res.statusCode);
36:
    }
37:
});
38:
 
39:
// Called when function is called with function's args.
40:
call.on('call', function(url) {
41:
    console.log('call: ' + util.inspect(arguments));
42:
});
43:
 
44:
// Called with results each time function returns.
45:
call.on('callback', function(err, res) {
46:
    console.log('callback: ' + util.inspect(arguments));
47:
});
48:
 
49:
// Called on backoff.
50:
call.on('backoff', function(number, delay) {
51:
    console.log('backoff: ' + util.inspect(arguments));
52:
});
53:
 
54:
call.setStrategy(new backoff.ExponentialStrategy());
55:
call.failAfter(2);
56:
call.start();