Node.js HTTP Request Loop with Queue
JS
S
JavaScriptTrivial request loop to mock POST payloads to endpoints. Using Axios for less verbosity.
1const axios = require('axios');
2
3const MAX_ITEMS = 100;
4
5function Queue(){
6 this.max = MAX_ITEMS;
7}
8Queue.prototype.next = function(){
9 if(this.max - 1 > 0) {
10 return this.max--;
11 } else {
12 return false;
13 }
14};
15
16
17function getImage(payload) {
18 console.log(payload)
19 return axios.post('https://ct.coderecipes.io/compiler/telemetry', payload)
20}
21
22
23const bluePrint = {
24 "id": "5c3f449411c3090011ab9fe1",
25 "feed": {
26 "price": "599.00 USD",
27 "image_link": "https://www.coderecipes.org/logo.png"
28 }
29};
30
31const q = new Queue();
32
33function runSerial(){
34 const query = q.next();
35 if(query) {
36 const payload = Object.assign(bluePrint, {});
37 payload.feed.price = String(Math.floor(Math.random() * (19999 - 222)) + query);
38 getImage(payload)
39 .then(res => {
40 runSerial();
41 })
42 .catch(err => {
43 console.error('Error', err.response.data);
44 })
45 } else {
46 console.log('finished');
47 }
48}
49
50runSerial();
51
52Created on 1/17/2019