2bce6f6ed9ab2d111541ffa13bb5daf0bb4c7a7f9df44733a38765bdeb636258bd65bb8a33d416ef75b218824cb69c643f4b5f9631c8d4c570ae711ad0faea 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { AsyncAction } from './AsyncAction';
  2. import { AsyncScheduler } from './AsyncScheduler';
  3. /**
  4. *
  5. * Async Scheduler
  6. *
  7. * <span class="informal">Schedule task as if you used setTimeout(task, duration)</span>
  8. *
  9. * `async` scheduler schedules tasks asynchronously, by putting them on the JavaScript
  10. * event loop queue. It is best used to delay tasks in time or to schedule tasks repeating
  11. * in intervals.
  12. *
  13. * If you just want to "defer" task, that is to perform it right after currently
  14. * executing synchronous code ends (commonly achieved by `setTimeout(deferredTask, 0)`),
  15. * better choice will be the {@link asapScheduler} scheduler.
  16. *
  17. * ## Examples
  18. * Use async scheduler to delay task
  19. * ```ts
  20. * import { asyncScheduler } from 'rxjs';
  21. *
  22. * const task = () => console.log('it works!');
  23. *
  24. * asyncScheduler.schedule(task, 2000);
  25. *
  26. * // After 2 seconds logs:
  27. * // "it works!"
  28. * ```
  29. *
  30. * Use async scheduler to repeat task in intervals
  31. * ```ts
  32. * import { asyncScheduler } from 'rxjs';
  33. *
  34. * function task(state) {
  35. * console.log(state);
  36. * this.schedule(state + 1, 1000); // `this` references currently executing Action,
  37. * // which we reschedule with new state and delay
  38. * }
  39. *
  40. * asyncScheduler.schedule(task, 3000, 0);
  41. *
  42. * // Logs:
  43. * // 0 after 3s
  44. * // 1 after 4s
  45. * // 2 after 5s
  46. * // 3 after 6s
  47. * ```
  48. */
  49. export const asyncScheduler = new AsyncScheduler(AsyncAction);
  50. /**
  51. * @deprecated Renamed to {@link asyncScheduler}. Will be removed in v8.
  52. */
  53. export const async = asyncScheduler;