UNPKG

futu-sdk

Version:

futu-api 的高性能精简易用版;基于*.proto静态编译,提供高性能的多层封装但层层开放的易用接口,获取最原始最完整的量化数据;相较于Python, nodejs更接近Web, 从而轻而易举搭建一个可视化交易站点,这是进行可控量化交易的不二选择。

44 lines (42 loc) 1.17 kB
export function* generator<T>( executor: (resolve: (value: T) => void, reject: (reason?: any) => void) => Promise<() => void> ) { type Executor<T> = { resolve: (value: T) => void; reject: (reason?: any) => void }; const error: { reason?: any; rejected?: boolean } = {}; const cache: Array<Promise<T> | Executor<T>> = []; const close = executor( value => { if (cache[0] && !(cache[0] instanceof Promise)) { (cache.shift() as Executor<T>).resolve(value); } else { cache.push(Promise.resolve(value)); } }, reason => { error.reason = reason; error.rejected = true; for (const item of cache) { if (!(item instanceof Promise)) { item.reject(reason); } } } ); try { while (true) { yield close.then(() => { if (cache[0] instanceof Promise) { return cache.shift() as Promise<T>; } if (error.rejected) { throw error.reason; } return new Promise<T>((resolve, reject) => { cache.push({ resolve, reject }); }); }); } } finally { close.then(close => close()); } }