alltherooms / cached-request

Node.js module to perform HTTP requests with caching support
MIT License
61 stars 23 forks source link

Sometimes the callback is not executed #31

Open elhoyos opened 5 years ago

elhoyos commented 5 years ago

Using

// test.js
const request = require('request')
const cr = require('./lib/index')(request)
cr.setCacheDirectory('/tmp/cache');

const log = (i) => (err, response, body) => {
  if (err) throw err;
  const cached = !!response.headers["x-from-cache"];
  console.log(`${i}: ${response.statusCode}, ${cached}`);
};

cr({url: 'https://www.google.com', ttl: 10000}, log(1))
cr({url: 'https://www.google.com', ttl: 10000}, log(2))

If I execute it after a non-cached response, these executions sometimes will result in nothing printed in the screen, and only after the cache expires I'm able to see results back again.

$ node test.js
1: 200, false
2: 200, false
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
$ node test.js
1: 200, false
2: 200, false
$ node test.js
1: 200, true
2: 200, true