async.js 599B

1234567891011121314151617181920212223242526272829303132333435
  1. var defer = require('./defer.js');
  2. // API
  3. module.exports = async;
  4. /**
  5. * Runs provided callback asynchronously
  6. * even if callback itself is not
  7. *
  8. * @param {function} callback - callback to invoke
  9. * @returns {function} - augmented callback
  10. */
  11. function async(callback)
  12. {
  13. var isAsync = false;
  14. // check if async happened
  15. defer(function() { isAsync = true; });
  16. return function async_callback(err, result)
  17. {
  18. if (isAsync)
  19. {
  20. callback(err, result);
  21. }
  22. else
  23. {
  24. defer(function nextTick_callback()
  25. {
  26. callback(err, result);
  27. });
  28. }
  29. };
  30. }