정홍주

postman에 코인리스트 10개를 업로드 할 수 있다

...@@ -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();
14 + function print_coin(){
15 + var promise = new Promise(function(resolve,reject){
16 + for(i=0;i<10;i++){
17 + (function(i){
18 + setTimeout(function(){
16 const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1'; 19 const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1';
17 const options = {method: 'GET', headers: {Accept: 'application/json'}}; 20 const options = {method: 'GET', headers: {Accept: 'application/json'}};
18 -
19 var coinJson = new Object(); 21 var coinJson = new Object();
20 -
21 fetch(url, options) 22 fetch(url, options)
22 - .then(req => req.json()) 23 + .then(res => res.json())
23 .then(json => { 24 .then(json => {
24 coinJson.coin = arr[i]; 25 coinJson.coin = arr[i];
25 - coinJson.price = json[0]["trade_price"]; 26 + coinJson.price = json[0].trade_price;
26 responseList.push(coinJson); 27 responseList.push(coinJson);
28 + //console.log(responseList)
29 + if(i>8){
30 + resolve(responseList);
31 + }
27 }) 32 })
28 .catch(err => console.error('error:' + err)); 33 .catch(err => console.error('error:' + err));
29 - 34 + },i*180);
35 + })(i);
30 } 36 }
31 -
32 -
33 - }).then(function(){
34 - jsonData = JSON.stringify(responseList);
35 - res.send(jsonData);
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.."); 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 + var promise = new Promise(function(resolve,reject){
12 + for(i=0;i<10;i++){
11 (function(i){ 13 (function(i){
12 setTimeout(function(){ 14 setTimeout(function(){
13 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';
14 const options = {method: 'GET', headers: {Accept: 'application/json'}}; 16 const options = {method: 'GET', headers: {Accept: 'application/json'}};
15 -
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 - })
23 - .then(function(){
24 responseList.push(coinJson); 23 responseList.push(coinJson);
25 - console.log(responseList); 24 + //console.log(responseList)
26 - 25 + if(i>8){
26 + resolve(responseList);
27 + }
27 }) 28 })
28 .catch(err => console.error('error:' + err)); 29 .catch(err => console.error('error:' + err));
29 - },10); 30 + },i*180);
30 })(i); 31 })(i);
31 -
32 } 32 }
33 + });
34 + return promise;
35 + }
36 + print_coin().then(function(resList){
37 + console.log(resList);
38 + });
33 39
34 }) 40 })
......
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