• after(n, func)

Called n times, only the last time

function after(n,callback) { return function (... args) { if(--n==0){ callback.apply(this,args); } } } let list = ['one','two']; let done = after(list.length,function(data){ console.log(data) }); done(list[0]); done(list[1]); // =>twoCopy the code

  • before(n, func)

Called n times, not executed the last time

function before(n, callback) { let result,count = n; return function (... args) { count = count -1; if (count > 0) result = callback.apply(this, args); if (count <= 1) result = undefined; return result }}let list1 = ['one', 'two','three']; let fn = before(3,function(data){ console.log(data)})fn(list1[0]); fn(list1[1]); fn(list1[2]); //=> one twoCopy the code