async.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function try_to_get_promise (value) {
  2. if (is(value, Types.Awaitable)) {
  3. return prms(value)
  4. } else {
  5. return value
  6. }
  7. }
  8. function try_to_forward_promise (value, resolve, reject) {
  9. if (is(value, Types.Awaitable)) {
  10. let p = prms(value)
  11. p.then(x => resolve(x))
  12. p.catch(e => reject(e))
  13. } else {
  14. resolve(value)
  15. }
  16. }
  17. pour(built_in_functions, {
  18. postpone: fun (
  19. 'function postpone (time: Size) -> Promise',
  20. time => new Promise(resolve => {
  21. setTimeout(() => resolve(Nil), time)
  22. })
  23. ),
  24. set_timeout: fun (
  25. 'function set_timeout (time: Size) -> Observer',
  26. time => observer(push => {
  27. let t = setTimeout(() => {
  28. push(Nil)
  29. push(Complete)
  30. }, time)
  31. return () => {
  32. clearTimeout(t)
  33. }
  34. })
  35. ),
  36. set_interval: fun (
  37. 'function set_interval (time: Size) -> Observer',
  38. time => observer(push => {
  39. let i = setInterval(() => {
  40. push(Nil)
  41. }, time)
  42. return () => {
  43. clearInterval(i)
  44. }
  45. })
  46. ),
  47. create_promise: fun (
  48. 'function create_promise (f: Arity<2>) -> Promise',
  49. f => {
  50. return new Promise((resolve, reject) => {
  51. let wrapped_resolve = fun (
  52. 'function resolve (value: Any) -> Void',
  53. value => {
  54. resolve(value)
  55. return Void
  56. }
  57. )
  58. let wrapped_reject = fun (
  59. 'function reject (error: Error) -> Void',
  60. error => {
  61. reject(error)
  62. return Void
  63. }
  64. )
  65. call(f, [wrapped_resolve, wrapped_reject])
  66. })
  67. }
  68. ),
  69. then: fun (
  70. 'function then (a: Awaitable, f: Arity<1>) -> Promise',
  71. (a, f) => {
  72. let p = prms(a)
  73. return p.then(value => {
  74. return try_to_get_promise(call(f, [value]))
  75. })
  76. },
  77. ),
  78. catch: fun (
  79. 'function catch (a: Awaitable, f: Arity<1>) -> Promise',
  80. (a, f) => {
  81. let p = prms(a)
  82. return p.catch(error => {
  83. if (is_fatal(error)) {
  84. throw error
  85. } else {
  86. return try_to_get_promise(call(f, [error]))
  87. }
  88. })
  89. }
  90. ),
  91. finally: fun (
  92. 'function finally (a: Awaitable, f: Arity<1>) -> Promise',
  93. (a, f) => {
  94. let p = prms(a)
  95. return new Promise((resolve, reject) => {
  96. p.then(value => {
  97. try {
  98. let ret = call(f, [value])
  99. try_to_forward_promise(ret, resolve, reject)
  100. } catch (error) {
  101. reject(error)
  102. }
  103. }, error => {
  104. if (is_fatal(error)) {
  105. reject(error)
  106. } else {
  107. try {
  108. let ret = call(f, [error])
  109. try_to_forward_promise(ret, resolve, reject)
  110. } catch (error) {
  111. reject(error)
  112. }
  113. }
  114. })
  115. })
  116. }
  117. )
  118. })