i going ask question again, because did not have right problem in mind last time.
suppose have collection of jquery selectors in array:
[{menu:'.menu-a',click:'.menu-a-click'},...]
i want iterate through these selectors, , if menu
not exist, if click
exists, click...
this trouble starts. need click once, timeout before clicking again - allows menu built script, , second click hide it.
i want use promises regulate clicking. setting chain of firecrackers, lighting first one... promises execute right away. how do this?
i can code in plain old js, want learn other methods. involving all
or map
, suppose.
here came with. beats three-legged donkey.
var menus=[{id:0},{id:1},{id:2},{id:3}]; function doer(menu){ return new promise(function called(res,rej){ console.log('click1:'+menu.id); settimeout(function(){ if(menu.id===2){return rej(new error('could not'));} console.log('click2:'+menu.id); res(1);// happy },math.random()*8000); }); } promise.reduce(menus,function(ctr,cur,i){ return doer(menus[i]).then(function(res,rej){ console.log('happy:',res); }); },$.when(1)).then(function(){ console.log('everyones done'); //no errors }).catch(function(eee){ console.log('error:',eee); }).finally(function(){ console.log('finally'); });
this bluebird if matters. don't know bluebird equivalent of $.when(1)
.
Comments
Post a Comment