From a41f7a5cd3de6f601f384a04444712009f711ed4 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Date: Thu, 25 Apr 2019 15:41:38 +0900 Subject: [PATCH] Add stream getting stock price & refactor --- src/helper.ts | 3 +-- src/index.ts | 1 + src/stock_api.ts | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/helper.ts b/src/helper.ts index 6e1aca3..77cc4d4 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -12,7 +12,7 @@ const fetchStock = async (stockCode: string): Promise => { const apiCall = () => { return new Promise((resolve, reject) => { const options = { - url: Helper.buildApiUrl(stockCode), + url: buildApiUrl(stockCode), method: 'GET', json: true }; @@ -28,6 +28,5 @@ const fetchStock = async (stockCode: string): Promise => { } export const Helper = { - buildApiUrl: buildApiUrl, fetchStock: fetchStock } diff --git a/src/index.ts b/src/index.ts index f8eb9fc..7d64a26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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: "); diff --git a/src/stock_api.ts b/src/stock_api.ts index 58d3a9b..ee81233 100644 --- a/src/stock_api.ts +++ b/src/stock_api.ts @@ -3,10 +3,8 @@ 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; @@ -14,7 +12,10 @@ const gets = (stockCodes: string[]) => { } 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 {