whats wrong with this code ? function callBacker(caller){ return function (thing1 ,…
- User
whats wrong with this code ?
function callBacker(caller){ return function (thing1 , thing2){ console.log("Our things are used here" , thing1 , thing2) caller(thing1, thing2) } }
function callB(thing1 , thing2){ console.log(thing1 , thing2) }
let thisCallback = callBacker(callB())
thisCallback("One" , "Two")
- Assistant
You’re almost there – the only hiccup is that you’re invoking
callBinstead of passing the function itself intocallBacker.Right now you have:
let thisCallback = callBacker( callB() );That actually calls
callBimmediately (with no arguments), so it returnsundefined. Thencallerinside your factory isundefined, and you get:TypeError: caller is not a functionThe fix
Pass the function reference, not the result of calling it:
let thisCallback = callBacker( callB ); // ← no parentheses here thisCallback("One", "Two");Full corrected snippet:
function callBacker(caller){ return function (thing1, thing2){ console.log("Our things are used here:", thing1, thing2); caller(thing1, thing2); } } function callB(thing1, thing2){ console.log("Inside callB:", thing1, thing2); } let thisCallback = callBacker(callB); thisCallback("One", "Two"); // → Our things are used here: One Two // → Inside callB: One TwoExplanation:
-
Passing vs. Invoking
callBis the function itself.callB()invokes it immediately and returns its result (here:undefined).
-
Higher-order factory
callBackerexpects a function (caller) and returns a new function that- logs your “Our things…” message
- then forwards onto
caller(thing1, thing2)
By handing it
callBdirectly, everything lines up. 👍 -
- User
why is this code not running the 1 second timeout and how can I fix that ?
function callBacker(caller){
console.log("This is done first") return function (thing1 , thing2){ console.log("Our things are used here" , thing1 , thing2) setTimeout = (() => { caller(thing1, thing2)} , 1000) }}
function callB(thing1 , thing2){ console.log(thing1 , thing2) }
let thisCallback = callBacker(callB)
thisCallback("One" , "Two")
- Assistant
The culprit is this line:
setTimeout = (() => { caller(thing1, thing2) }, 1000)By using the assignment operator (
=), you’re overwriting the globalsetTimeoutfunction instead of calling it. After that line runs, nothing actually schedules your callback for 1 second—and you’ve destroyed the realsetTimeoutgoing forward!–––
✅ Quick Fix
Call
setTimeoutinstead of assigning to it:function callBacker(caller) { console.log("This is done first"); return function (thing1, thing2) { console.log("Our things are used here", thing1, thing2); // ← call setTimeout, don’t overwrite it setTimeout(() => { caller(thing1, thing2); }, 1000); }; } function callB(thing1, thing2) { console.log("Inside callB:", thing1, thing2); } let thisCallback = callBacker(callB); thisCallback("One", "Two"); // → This is done first // → Our things are used here One Two // (waits 1s) // → Inside callB: One TwoWhat went wrong?
setTimeout = (…)assigns a new value to thesetTimeoutidentifier.- You never actually invoke the original timer function.
- Future calls to
setTimeoutwill be broken because you stomped on it.
By simply removing the
=and usingsetTimeout(…)as intended, your callback will fire after the desired 1 second delay. 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.