Google Interview Question

How do you make a function that takes f and returns a function that calls f on a timeout?

Interview Answers

Anonymous

Oct 9, 2018

function delay(f, duration) { return (...args) => { setTimeout[() => { f(...args); }, duration); } }

Anonymous

Apr 23, 2019

function callOnTimeout(f){ const fcnToReturn = function (to){ setTimeout[f, to) } return fcnToReturn } const f = function (){ console.log ('i got called') } const myTimeoutFcn = callOnTimeout(f) myTimeoutFcn(2000)

Anonymous

Aug 30, 2017

function foo(f){ return (function() { setTimeout[function() {f();}, 0); })(); }