Skip to content

Instantly share code, notes, and snippets.

@xiaoxiangmoe
Created May 18, 2020 17:28
Show Gist options
  • Save xiaoxiangmoe/544e1db4ac9ea8e691673d98f52ba5a3 to your computer and use it in GitHub Desktop.
Save xiaoxiangmoe/544e1db4ac9ea8e691673d98f52ba5a3 to your computer and use it in GitHub Desktop.
问题:用此API打印出从1到30的十进制转十六制的对应表(最好按顺序打印)
// https://coolshell.cn/t.html
const scan = (xs, func, init) =>
xs.reduce(
(accu, curr, i, arr) => [
...accu,
func(accu[accu.length - 1], curr, i, arr),
],
[init],
);
scan(
Array.from({ length: 20 }).map(
(x, i) => new Promise(resolve => xss_rpc_call2(i, resolve)),
),
(prev, curr) =>
Promise.all([prev, curr]).then(([xs, x]) => [...xs, x]),
Promise.resolve([]),
).forEach(x => x.then(console.log));
@xiaoxiangmoe
Copy link
Author

xiaoxiangmoe commented May 18, 2020

如果只需要最后再显示,改 scan 成 Promise.all 即可。
如果需要超时逻辑,添加一个 Promise.race 即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment