Skip to content

Instantly share code, notes, and snippets.

@freeart
Last active August 22, 2017 07:43
Show Gist options
  • Select an option

  • Save freeart/0a83b38fb0d0cc0f1799ed6703afd431 to your computer and use it in GitHub Desktop.

Select an option

Save freeart/0a83b38fb0d0cc0f1799ed6703afd431 to your computer and use it in GitHub Desktop.
const request = require('request');
const async = require('async');
function send(data, cb) {
request(data, (err, res, body) => {
if (!err && (res.statusCode > 0 && res.statusCode < 400)) {
return cb && cb(null, body)
}
if (!err && (res.statusCode >= 400 || res.statusCode <= 0)) {
console.error(res.statusCode)
return cb && cb("status " + res.statusCode)
}
if (err) {
console.error(err.toString())
}
cb && cb(err);
});
}
//right way to control async flow
async.timesLimit(1000, 10, (index, cb)=>{
async.retry({times: 3, interval: 100}, (cb)=> {
send({
uri: "http://jsonplaceholder.typicode.com/posts",
method: 'POST',
json: true,
followAllRedirects: true,
body: {
title: 'foo',
body: 'bar',
userId: 1
},
timeout: 5000
}, cb)
}, cb);
}, (err)=>{
console.log("done")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment