after源码
function after(n, func) {
if (typeof func !== 'function') {
throw new TypeError('Expected a function')
}
n = n || 0
return function(...args) {
if (--n < 1) {
return func.apply(this, args)
}
}
}
主要用于异步操作,当调用多少次后可以返回最后给出的内容,该代码上有写调用方法如下所示
const saves = ['profile', 'settings']
const done = after(saves.length, () => console.log('done saving!'))
forEach(saves, type => asyncSave({ 'type': type, 'complete': done }))
当自己使用时,需要写出asyncSave方法,感觉类似于generator函数吧,不知道怎么使用……