utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. * async.js
  3. * Copyright(c) 2010 Fabian Jakobs <fabian.jakobs@web.de>
  4. * MIT Licensed
  5. */
  6. define(function(require, exports, module) {
  7. var async = require("asyncjs/async")
  8. async.plugin({
  9. delay: function(delay) {
  10. return this.each(function(item, next) {
  11. setTimeout(function() {
  12. next();
  13. }, delay)
  14. })
  15. },
  16. timeout: function(timeout) {
  17. timeout = timeout || 0
  18. var source = this.source
  19. this.next = function(callback) {
  20. var called
  21. var id = setTimeout(function() {
  22. called = true
  23. callback("Source did not respond after " + timeout + "ms!")
  24. }, timeout)
  25. source.next(function(err, value) {
  26. if (called)
  27. return
  28. called = true
  29. clearTimeout(id)
  30. callback(err, value)
  31. })
  32. }
  33. return new this.constructor(this)
  34. },
  35. get: function(key) {
  36. return this.map(function(value, next) {
  37. next(null, value[key])
  38. })
  39. },
  40. inspect: function() {
  41. return this.each(function(item, next) {
  42. console.log(JSON.stringify(item))
  43. next()
  44. })
  45. },
  46. print: function() {
  47. return this.each(function(item, next) {
  48. console.log(item)
  49. next()
  50. })
  51. }
  52. })
  53. })