Showing
3 changed files
with
95 additions
and
53 deletions
... | @@ -7,33 +7,42 @@ app.use(bodyParser.urlencoded({ extended: false })); | ... | @@ -7,33 +7,42 @@ app.use(bodyParser.urlencoded({ extended: false })); |
7 | app.use(bodyParser.json()); | 7 | app.use(bodyParser.json()); |
8 | 8 | ||
9 | app.get('/price', function(req, res) { | 9 | app.get('/price', function(req, res) { |
10 | - var arr ; | ||
11 | - var responseList = new Array(); | ||
12 | fs.readFile('coin_name.txt', 'utf8', function(err,data){ | 10 | fs.readFile('coin_name.txt', 'utf8', function(err,data){ |
13 | arr = data.split(","); | 11 | arr = data.split(","); |
14 | - console.log(arr); | 12 | + // console.log(arr); |
15 | - for(var i=0;i<1;i++){ | 13 | + var responseList = new Array(); |
16 | - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; | 14 | + function print_coin(){ |
17 | - const options = {method: 'GET', headers: {Accept: 'application/json'}}; | 15 | + var promise = new Promise(function(resolve,reject){ |
18 | - | 16 | + for(i=0;i<10;i++){ |
19 | - var coinJson = new Object(); | 17 | + (function(i){ |
20 | - | 18 | + setTimeout(function(){ |
21 | - fetch(url, options) | 19 | + const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; |
22 | - .then(req => req.json()) | 20 | + const options = {method: 'GET', headers: {Accept: 'application/json'}}; |
23 | - .then(json => { | 21 | + var coinJson = new Object(); |
24 | - coinJson.coin = arr[i]; | 22 | + fetch(url, options) |
25 | - coinJson.price = json[0]["trade_price"]; | 23 | + .then(res => res.json()) |
26 | - responseList.push(coinJson); | 24 | + .then(json => { |
27 | - }) | 25 | + coinJson.coin = arr[i]; |
28 | - .catch(err => console.error('error:' + err)); | 26 | + coinJson.price = json[0].trade_price; |
29 | - | 27 | + responseList.push(coinJson); |
30 | - } | 28 | + //console.log(responseList) |
31 | - | 29 | + if(i>8){ |
32 | - | 30 | + resolve(responseList); |
33 | - }).then(function(){ | 31 | + } |
34 | - jsonData = JSON.stringify(responseList); | 32 | + }) |
35 | - res.send(jsonData); | 33 | + .catch(err => console.error('error:' + err)); |
36 | - }); | 34 | + },i*180); |
35 | + })(i); | ||
36 | + } | ||
37 | + }); | ||
38 | + return promise; | ||
39 | + } | ||
40 | + print_coin().then(function(resList){ | ||
41 | + res.send(resList); | ||
42 | + }); | ||
43 | + | ||
44 | + }) | ||
37 | }) | 45 | }) |
38 | var server = app.listen(8082); | 46 | var server = app.listen(8082); |
39 | -console.log("Server Created.."); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
47 | +console.log("Server Created.."); | ||
48 | + | ... | ... |
1 | const fetch = require('node-fetch'); | 1 | const fetch = require('node-fetch'); |
2 | var fs = require('fs'); | 2 | var fs = require('fs'); |
3 | 3 | ||
4 | -var responseList = new Array(); | 4 | + |
5 | + | ||
5 | fs.readFile('coin_name.txt', 'utf8', function(err,data){ | 6 | fs.readFile('coin_name.txt', 'utf8', function(err,data){ |
6 | arr = data.split(","); | 7 | arr = data.split(","); |
7 | - | ||
8 | // console.log(arr); | 8 | // console.log(arr); |
9 | - | 9 | + var responseList = new Array(); |
10 | - for(var i=0;i<10;i++){ | 10 | + function print_coin(){ |
11 | - (function(i){ | 11 | + var promise = new Promise(function(resolve,reject){ |
12 | - setTimeout(function(){ | 12 | + for(i=0;i<10;i++){ |
13 | - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; | 13 | + (function(i){ |
14 | - const options = {method: 'GET', headers: {Accept: 'application/json'}}; | 14 | + setTimeout(function(){ |
15 | - | 15 | + const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; |
16 | - var coinJson = new Object(); | 16 | + const options = {method: 'GET', headers: {Accept: 'application/json'}}; |
17 | - fetch(url, options) | 17 | + var coinJson = new Object(); |
18 | - .then(res => res.json()) | 18 | + fetch(url, options) |
19 | - .then(json => { | 19 | + .then(res => res.json()) |
20 | - coinJson.coin = arr[i]; | 20 | + .then(json => { |
21 | - coinJson.price = json[0].trade_price; | 21 | + coinJson.coin = arr[i]; |
22 | - }) | 22 | + coinJson.price = json[0].trade_price; |
23 | - .then(function(){ | 23 | + responseList.push(coinJson); |
24 | - responseList.push(coinJson); | 24 | + //console.log(responseList) |
25 | - console.log(responseList); | 25 | + if(i>8){ |
26 | - | 26 | + resolve(responseList); |
27 | - }) | 27 | + } |
28 | - .catch(err => console.error('error:' + err)); | 28 | + }) |
29 | - },10); | 29 | + .catch(err => console.error('error:' + err)); |
30 | - })(i); | 30 | + },i*180); |
31 | - | 31 | + })(i); |
32 | - } | 32 | + } |
33 | - | 33 | + }); |
34 | + return promise; | ||
35 | + } | ||
36 | + print_coin().then(function(resList){ | ||
37 | + console.log(resList); | ||
38 | + }); | ||
39 | + | ||
34 | }) | 40 | }) | ... | ... |
test2.js
0 → 100644
1 | +// for(var i =0; i<10;i++){ | ||
2 | +// (function(i){ | ||
3 | +// setTimeout(function(){ | ||
4 | +// console.log(i); | ||
5 | +// },10); | ||
6 | +// })(i); | ||
7 | +// } | ||
8 | + | ||
9 | + function f() { | ||
10 | + var promise = new Promise(function(resolve,reject) { | ||
11 | + for(var i =0; i<10;i++){ | ||
12 | + (function(i){ | ||
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 | + | ||
27 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment