Skip to content

Commit

Permalink
better look of result
Browse files Browse the repository at this point in the history
  • Loading branch information
g-viet committed Jul 2, 2019
1 parent 0dd72e3 commit 2bb6e62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fetchStock = async (stockCode: string): Promise<any> => {
headers: {
"Content-Type": "application/json"
},
body: { "code": stockCode, "s": "1", "t": getCurrentTime() }
body: { code: stockCode, s: "1", t: getCurrentTime() }
};
request(options, (err: any, _res: any, body: any) => {
if (err) reject(err);
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ program
StockApi.gets(stocks);
});
program
.command('stream <stockCode>')
.command('stream <stocks...>')
.description('get streaming stock price')
.action((stockCode: string) => {
StockApi.stream(stockCode);
.action((stocks: string[]) => {
StockApi.stream(stocks);
});
program
.command('group [groupCode]')
Expand Down
17 changes: 12 additions & 5 deletions src/stock_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import { Stock } from './stock';
const gets = (stockCodes: string[]) => {
Stock.printfHeader();
try {
return Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code).then(stock => stock && stock.printf());
}));
Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code);
})).then(stocks =>
stocks.map(stock => stock && stock.printf())
);
} catch (err) {
throw err;
}
}

const stream = (stockCode: string) => {
const stream = (stockCodes: string[]) => {
Stock.printfHeader();
setInterval(() => {
Helper.fetchStock(stockCode).then(stock => stock && stock.printf());
Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code);
})).then(stocks =>
stocks.map(stock => stock && stock.printf())
);
// Helper.fetchStock(stockCode).then(stock => stock && stock.printf());
}, 2000);
}

Expand Down

0 comments on commit 2bb6e62

Please sign in to comment.