实现异步事件A-B-C依次执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| const A = () => { console.log('A'); }; const B = () => { console.log('B'); }; const C = () => { console.log('C'); }; const myPromise = (callback, delay) => { return new Promise((resolve, reject) => { setTimeout(() => { callback(); resolve(); }, delay); }); };
const loop = () => myPromise(A, 3000).then(() => myPromise(B, 2000)).then(() => myPromise(C, 1000)).then(loop);
|
请你实现异步接口C,它的参数基于异步接口A的user_name以及异步接口B的mobile
1 2 3 4 5 6
| Promise.all([ fetch('A').then(res => res.json()) fetch('B').then(res => res.json()) ]).then((res) => { fetch(`C?user_name=${res[0].user_name}&mobile=${res[1].mobile}`).then(res => res.json()); });
|
实现一个探测 CPU 占比的方法
根据 setInterval 的时间间隔,根据最后输出的时间比 1000ms 多多少,越多代表 CPU 占比越高。
1 2 3 4 5 6 7 8 9 10
| let start = Date.now(); const cpuTimer = setInterval(() => { const cb = () => { const end = Date.now(); const time = end - start; start = end; console.log(`CPU 占比:${time}ms`); }; cb(); }, 1000);
|