Skip to content

Commit

Permalink
Add stream getting stock price & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
g-viet committed Apr 25, 2019
1 parent 3d35912 commit a41f7a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fetchStock = async (stockCode: string): Promise<Stock> => {
const apiCall = () => {
return new Promise((resolve, reject) => {
const options = {
url: Helper.buildApiUrl(stockCode),
url: buildApiUrl(stockCode),
method: 'GET',
json: true
};
Expand All @@ -28,6 +28,5 @@ const fetchStock = async (stockCode: string): Promise<Stock> => {
}

export const Helper = {
buildApiUrl: buildApiUrl,
fetchStock: fetchStock
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program
return;
}
Stock.gets(stocks);
break;
case "stream":
if (stocks.length == 0) {
console.log("You must include stock code!! For example: ");
Expand Down
11 changes: 6 additions & 5 deletions src/stock_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { Helper } from './helper';
const gets = (stockCodes: string[]) => {
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
try {
return Promise.all(stockCodes.map(async (code) => {
const stock = await Helper.fetchStock(code);
stock.printf();
return true;
return Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code).then(stock => stock.printf());
}));
} catch (err) {
throw err;
}
}

const stream = (stockCode: string) => {
return;
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
setInterval(() => {
Helper.fetchStock(stockCode).then(stock => stock.printf());
}, 2000);
}

export {
Expand Down

0 comments on commit a41f7a5

Please sign in to comment.