Last active
August 22, 2017 07:43
-
-
Save freeart/0a83b38fb0d0cc0f1799ed6703afd431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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