javascript - Callback can not be invoked properly, if the parameter is a bound function -


when function expects callback param, suppose make sense provide bound function this

function invokecb(cb){     cb(); } function test(x){     console.log(x) } const para="xyz";  invokecb(test.bind(null,para)) //(1) invokecb(()=>{test(para)}) //(2) 

i can not see wrong (1). however, when come real world, encounter unexpected behaviour

here example, in redux

store.subscribe(     ()=>{(savestate.bind(null,store.getstate()))()} ) 

can work while

store.subscribe(savestate.bind(null,store.getstate())) 

can not correctly, i.e. store.getstate() seems never invoked properly

if need more context. here:https://egghead.io/lessons/javascript-redux-persisting-the-state-to-the-local-storage

maybe missed subtle differences between 2 form, can point out?

the differences are

  • the time @ store.getstate() evaluated
  • the return value (by using braces without return, suppress it)

let data = "abc"; function invokecb(cb){     data = "xyz";     console.log(cb()); } function test(x){     console.log(x);     return x.touppercase(); } invokecb(test.bind(null, data)); // abc abc invokecb(()=>{ test(data); }); // xyz undefined 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -