[Javascript] Coding interview problem: Scheduler functional way

时间:2023-03-08 23:29:57
[Javascript] Coding interview problem: Scheduler functional way

Implement a job scheduler which takes in a function f and an integer n, and calls f after nmilliseconds

function curry (fn) {
const arity = fn.length;
return function $curry(...args) {
if (args.length < arity) {
return $curry.bind(null, ...args);
} return fn.call(null, ...args);
}
} const setScheduler = curry((ms, fn) => {
return setTimeout(() => fn(), ms)
}); const halfSecond = setScheduler(); console.log('before');
halfSecond(() => console.log('Timeup'));
setTimeout(() => console.log('after'), );
// |A A: before
// |-----B B: Timeup
// |------C C: after