javascript - Avoiding repetitive creation of functions when calling async functions in loop -


is there way following without creating anonymous function 10 times? assuming settimeout takes 2 parameters, callback , interval. i've heard shouldn't create functions in loop since it's slow. however, if asynchronous function doesn't allow pass parameters want callback know about, possible avoid doing so? note: know in chrome settimeout takes more params, isn't ideal example.

function dosomethingasync(i){     settimeout(function() { //this anonymous function created 10 times         console.log(i);     }, 1000); }  (var = 0; < 10; i++) {     dosomethingasync(i); } 

short answer - no. not if want match each iteration's value mapped back, doing here. not going comment performance here, rather focus on need first.

if don't care value being mapped right call, can avoid creating functions simply

there cases javascript engines allow. apply not having define multiple anonymous functions. achieved providing more info regarding current execution callback, i.e. eventobject. how dom, xhrs, html5 file apis, etc., interact back. ideally means outside javascript context calling javascript method required info.

the browser or rather js engine should provide mechanism predefine sorta eventobject callback strategy in general, 1 function can handle callbacks. afaik, there no present way same in js itself, apart creating small function closures. gotta wait till there news on it.

hope helps shed light on it.

update - saw note on question, removing unnecessary code.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -