Skip to content

Commit

Permalink
to make Promise return in getKData
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppeter committed May 24, 2017
1 parent f949c8d commit 79056f3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 65 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ stock.getTick(options).then(({ data }) => {

### 获取历史K线数据
```
const callback = function cb(data,args) {
const callback = function cb(data) {
console.log('code %s',args);
console.log(data);
};
Expand All @@ -77,10 +77,15 @@ const options = {
index: true,
start : '2015-01-01',
end: '2016-10-22',
cb: callback,
args: '000001'
};
stock.getKData(options);
stock.getKData(options).then(data => {
console.log('code %s',options.code);
console.log(data);
})
.catch(err => {
console.error('get %s error %s', options.code, err);
});
```

`options` 参数说明:
Expand All @@ -89,8 +94,6 @@ stock.getKData(options);
start: {string} 起始时间,格式YYYY-MM-DD
end: {string} 结束时间,格式YYYY-MM-DD
code: {string} 股票代码
cb: {function} 回调函数
args: {object} 回调使用的参数
ktype: {string} K线类型 ('day','week','month','5','10','15','30','60')
index: {bool} 表示是否是指数,默认是否
}
Expand Down
18 changes: 9 additions & 9 deletions example/getkdata/getk.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ const parser = extargsparse.ExtArgsParse();
parser.load_command_line_string(command);
const args = parser.parse_command_line();

const debugFunction = function df(data,args) {
console.log('code %s',args);
data.forEach(function(d) {
console.log('%s',d);
});
};

args.args.forEach(function(code) {
let options = {};
Expand All @@ -41,8 +35,14 @@ args.args.forEach(function(code) {
options.end = args.end;
options.ktype = args.ktype;
options.autype = args.autype;
options.cb = debugFunction;
options.args = code;
options.index = args.index;
stock.getKData(options);
stock.getKData(options).then(data => {
console.log('code %s',code);
data.forEach(function(d) {
console.log('%s',d);
});
})
.catch(err => {
console.error('get %s error %s', code, err);
});
});
82 changes: 37 additions & 45 deletions src/stock/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ const _getKDataLong = (options = {}) => {

handleddata = [];
handledurls = [];
urls.forEach(elmurl => {
fetch(elmurl)
.then(checkStatus)
.then(charset('ascii'))
.then(dictdata => {
handledurls.push(elmurl);
if (handledurls.length === urls.length) {
_storeListData(dictdata, handleddata, options.ktype, symbol, setdata => {
if (options.cb !== null) {
return new Promise((resolve, reject) => {
urls.forEach(elmurl => {
fetch(elmurl)
.then(checkStatus)
.then(charset('ascii'))
.then(dictdata => {
handledurls.push(elmurl);
if (handledurls.length === urls.length) {
_storeListData(dictdata, handleddata, options.ktype, symbol, setdata => {
const kdata = [];
const stick = _getTimeTick(sdate);
const etick = _getTimeTick(edate);
Expand All @@ -255,16 +255,16 @@ const _getKDataLong = (options = {}) => {
kdata.push(curdata);
}
});
options.cb(kdata, options.args);
}
});
} else {
_storeListData(dictdata, handleddata, options.ktype, symbol);
}
})
.catch(error => {
throw new Error(util.format('error for %s %s', elmurl, error));
});
resolve(kdata);
});
} else {
_storeListData(dictdata, handleddata, options.ktype, symbol);
}
})
.catch(err => {
reject(err);
});
});
});
};

Expand All @@ -288,30 +288,29 @@ const _getKDataShort = (options = {}) => {

handleddata = [];
ktype = util.format('m%s', options.ktype);
fetch(url)
.then(checkStatus)
return new Promise((resolve, reject) => {
fetch(url)
.then(checkStatus)
.then(charset('ascii'))
.then(dictdata => {
_storeListData(dictdata, handleddata, ktype, symbol, setdata => {
if (options.cb !== null) {
const kdata = [];
const stick = _getTimeTick(sdate);
const etick = _getTimeTick(edate);

setdata.forEach(curdata => {
const curtick = _getTimeTick(curdata[0]);
if (curtick >= stick && curtick <= etick) {
kdata.push(curdata);
}
});

options.cb(kdata, options.args);
}
const kdata = [];
const stick = _getTimeTick(sdate);
const etick = _getTimeTick(edate);

setdata.forEach(curdata => {
const curtick = _getTimeTick(curdata[0]);
if (curtick >= stick && curtick <= etick) {
kdata.push(curdata);
}
});
resolve(kdata);
});
})
.catch(error => {
throw new Error(util.format('error for %s %s', url, error));
.catch(err => {
reject(err);
});
});
};


Expand All @@ -326,10 +325,7 @@ const _getKDataShort = (options = {}) => {
* @param {String} options.ktype - 数据类型,day=日k线 week=周 month=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为day
* @param {String} options.autype - 复权类型,默认前复权, fq=前复权, last=不复权
* @param {Bool} options.index - 是否为指数,默认为false
* @param {function} options.cb - 回调函数,得到全部数据之后的处理函数
* @param {object} options.args - 回调参数,是回调函数的输入参数
* @param cb
* @return {undefined}
* @return {Promise object} Promise Object 可以调用 then catch函数
*/
export const getKData = (query = {}) => {
const defaults = {
Expand All @@ -339,15 +335,11 @@ export const getKData = (query = {}) => {
ktype: 'day',
autype: 'fq',
index: false,
args: null,
};


const options = Object.assign({}, defaults, query);

if (options.cb === undefined || options.cb === null) {
throw new Error('not define cb in callback');
}

if (cons.K_LABELS.includes(options.ktype)) {
return _getKDataLong(options);
Expand Down
20 changes: 14 additions & 6 deletions test/getkdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ test.cb('Get Special case for long time ago', t => {
opt.code = '000002';
opt.start = '2002-01-01';
opt.end = '2002-04-06';
const callback = function cb(data, args) {
const callback = function cb(data) {
t.truthy(data.length >= 20, 'get data length 20');
t.end();
};
opt.cb = callback;
opt.args = null;
stock.getKData(opt);
stock.getKData(opt).then(callback).catch(err => {
t.truthy(false, util.format('long time getkdata error %s', err));
});
});

const _getTimeTick = ts => {
Expand Down Expand Up @@ -52,7 +54,7 @@ test.cb('Get short time index', t => {
opt.ktype = '5';
opt.start = sdate;
opt.end = edate;
const callback = function cb(data, args) {
const callback = function cb(data) {
let curtick;
const stick = _getTimeTick(sdate);
const etick = _getTimeTick(edate);
Expand All @@ -68,7 +70,10 @@ test.cb('Get short time index', t => {
};
opt.cb = callback;
opt.args = null;
stock.getKData(opt);
stock.getKData(opt).then(callback).catch( err => {
t.truthy(false, util.format('short time index error %s', err));
t.end();
});
});

test.cb('get data for sequence', t => {
Expand All @@ -82,7 +87,7 @@ test.cb('get data for sequence', t => {
opt.ktype = '5';
opt.start = sdate;
opt.end = edate;
const callback = function cb(data, args) {
const callback = function cb(data) {
let curtick;
let idx = 0;
let lastval = 0;
Expand All @@ -101,5 +106,8 @@ test.cb('get data for sequence', t => {
};
opt.cb = callback;
opt.args = null;
stock.getKData(opt);
stock.getKData(opt).then(callback).catch(err => {
t.truthy( false , util.format('get sequence error %s', err));
t.end();
});
});

0 comments on commit 79056f3

Please sign in to comment.