after.js 581 B

12345678910111213141516171819202122232425
  1. var slice = require('./slice')
  2. /**
  3. * 创建一个函数, 调用次数超过 count 次之后执行回调并将所有结果记住后返回
  4. *
  5. * @param {Number} count 调用次数
  6. * @param {Function} callback 完成回调
  7. * @return {Object}
  8. */
  9. function after (count, callback, context) {
  10. var runCount = 0
  11. var rests = []
  12. return function () {
  13. var args = arguments
  14. runCount++
  15. if (runCount <= count) {
  16. rests.push(args[0])
  17. }
  18. if (runCount >= count) {
  19. callback.apply(context, [rests].concat(slice(args)))
  20. }
  21. }
  22. }
  23. module.exports = after