Showing
3 changed files
with
63 additions
and
76 deletions
... | @@ -13,34 +13,36 @@ app.get('/price', function(req, res) { | ... | @@ -13,34 +13,36 @@ app.get('/price', function(req, res) { |
13 | var responseList = new Array(); | 13 | var responseList = new Array(); |
14 | function print_coin(){ | 14 | function print_coin(){ |
15 | var promise = new Promise(function(resolve,reject){ | 15 | var promise = new Promise(function(resolve,reject){ |
16 | - for(i=0;i<10;i++){ | 16 | + for(j=0;j<arr.length/10;j++){ |
17 | - (function(i){ | 17 | + for(i=j*10;i<(j+1)*10;i++){ |
18 | - setTimeout(function(){ | 18 | + (function(i){ |
19 | - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; | 19 | + setTimeout(function(){ |
20 | - const options = {method: 'GET', headers: {Accept: 'application/json'}}; | 20 | + const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; |
21 | - var coinJson = new Object(); | 21 | + const options = {method: 'GET', headers: {Accept: 'application/json'}}; |
22 | - fetch(url, options) | 22 | + var coinJson = new Object(); |
23 | - .then(res => res.json()) | 23 | + fetch(url, options) |
24 | - .then(json => { | 24 | + .then(res => res.json()) |
25 | - coinJson.coin = arr[i]; | 25 | + .then(json => { |
26 | - coinJson.price = json[0].trade_price; | 26 | + coinJson.coin = arr[i]; |
27 | - responseList.push(coinJson); | 27 | + coinJson.price = json[0].trade_price; |
28 | - //console.log(responseList) | 28 | + responseList.push(coinJson); |
29 | - if(i>8){ | 29 | + console.log(i) |
30 | - resolve(responseList); | 30 | + if(i>115){ |
31 | - } | 31 | + resolve(responseList); |
32 | - }) | 32 | + } |
33 | - .catch(err => console.error('error:' + err)); | 33 | + }) |
34 | - },i*180); | 34 | + .catch(err => console.error('error:' + err)); |
35 | - })(i); | 35 | + },i*180); |
36 | + })(i); | ||
37 | + } | ||
36 | } | 38 | } |
39 | + | ||
37 | }); | 40 | }); |
38 | return promise; | 41 | return promise; |
39 | } | 42 | } |
40 | print_coin().then(function(resList){ | 43 | print_coin().then(function(resList){ |
41 | res.send(resList); | 44 | res.send(resList); |
42 | - }); | 45 | + }); |
43 | - | ||
44 | }) | 46 | }) |
45 | }) | 47 | }) |
46 | var server = app.listen(8082); | 48 | var server = app.listen(8082); | ... | ... |
... | @@ -7,34 +7,27 @@ fs.readFile('coin_name.txt', 'utf8', function(err,data){ | ... | @@ -7,34 +7,27 @@ fs.readFile('coin_name.txt', 'utf8', function(err,data){ |
7 | arr = data.split(","); | 7 | arr = data.split(","); |
8 | // console.log(arr); | 8 | // console.log(arr); |
9 | var responseList = new Array(); | 9 | var responseList = new Array(); |
10 | - function print_coin(){ | 10 | + for(j=0;j<2;j++){ |
11 | - var promise = new Promise(function(resolve,reject){ | 11 | + for(i=j*10;i<(j+1)*10;i++){ |
12 | - for(i=0;i<10;i++){ | 12 | + (function(i){ |
13 | - (function(i){ | 13 | + setTimeout(function(){ |
14 | - setTimeout(function(){ | 14 | + const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; |
15 | - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; | 15 | + const options = {method: 'GET', headers: {Accept: 'application/json'}}; |
16 | - const options = {method: 'GET', headers: {Accept: 'application/json'}}; | 16 | + var coinJson = new Object(); |
17 | - var coinJson = new Object(); | 17 | + fetch(url, options) |
18 | - fetch(url, options) | 18 | + .then(res => res.json()) |
19 | - .then(res => res.json()) | 19 | + .then(json => { |
20 | - .then(json => { | 20 | + coinJson.coin = arr[i]; |
21 | - coinJson.coin = arr[i]; | 21 | + coinJson.price = json[0].trade_price; |
22 | - coinJson.price = json[0].trade_price; | 22 | + responseList.push(coinJson); |
23 | - responseList.push(coinJson); | 23 | + // console.log(responseList) |
24 | - //console.log(responseList) | 24 | + console.log(i) |
25 | - if(i>8){ | 25 | + }) |
26 | - resolve(responseList); | 26 | + .catch(err => console.error('error:' + err)); |
27 | - } | 27 | + },i*180); |
28 | - }) | 28 | + })(i); |
29 | - .catch(err => console.error('error:' + err)); | 29 | + } |
30 | - },i*180); | 30 | + } |
31 | - })(i); | 31 | + |
32 | - } | ||
33 | - }); | ||
34 | - return promise; | ||
35 | - } | ||
36 | - print_coin().then(function(resList){ | ||
37 | - console.log(resList); | ||
38 | - }); | ||
39 | 32 | ||
40 | }) | 33 | }) | ... | ... |
1 | -// for(var i =0; i<10;i++){ | 1 | +const delay = () => { |
2 | -// (function(i){ | 2 | + const randomDelay = Math.floor(Math.random() * 4) * 100 |
3 | -// setTimeout(function(){ | 3 | + return new Promise(resolve => setTimeout(resolve, randomDelay)) |
4 | -// console.log(i); | 4 | + } |
5 | -// },10); | 5 | + |
6 | -// })(i); | 6 | +const loop = async (list) => { |
7 | -// } | 7 | +console.log("시작") |
8 | +const promises = list.map(async data => { | ||
9 | + return await delay() | ||
10 | + .then(() => data) | ||
11 | +}) | ||
8 | 12 | ||
9 | - function f() { | 13 | +const results = await Promise.all(promises) |
10 | - var promise = new Promise(function(resolve,reject) { | 14 | +results.forEach(data => console.log(data)) |
11 | - for(var i =0; i<10;i++){ | 15 | +console.log("끝") |
12 | - (function(i){ | 16 | +} |
13 | - setTimeout(function(){ | ||
14 | - if(i>8){ | ||
15 | - resolve(i); | ||
16 | - } | ||
17 | - },10); | ||
18 | - })(i); | ||
19 | - } | ||
20 | - }); | ||
21 | - return promise; | ||
22 | - } | ||
23 | - f().then(function(a) { | ||
24 | - console.log(a); | ||
25 | - }); | ||
26 | 17 | ||
27 | - | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
18 | +const list = [1, 2, 3, 4, 5, 6, 7] | ||
19 | +loop(list) | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment