项目作者: andy2046

项目描述 :
functional promise with operators Map Filter Reduce Scan and more
高级语言: JavaScript
项目地址: git://github.com/andy2046/promisu.git
创建时间: 2017-11-17T03:17:40Z
项目社区:https://github.com/andy2046/promisu

开源协议:MIT License

下载


promisu

promisu is a JavaScript lib for functional promise with the following operators: Map, Filter, Reduce, Scan, Every, Some, Few, Finally, All, Race, Try, WaitFor, Queue, Debounce, Throttle.

Examples

  1. import {
  2. PromisuAll,
  3. PromisuMap,
  4. PromisuEvery,
  5. PromisuFew,
  6. PromisuFilter,
  7. PromisuFinally,
  8. PromisuQueue,
  9. PromisuRace,
  10. PromisuReduce,
  11. PromisuSome,
  12. PromisuTry,
  13. PromisuWaitFor,
  14. PromisuScan,
  15. PromisuDebounce,
  16. PromisuThrottle
  17. } from 'promisu';
  18. const asyncTask = (time) => () => { return new Promise(resolve => {
  19. setTimeout(() => resolve(time), time)
  20. })};
  21. const testPromisuQueue = () => new Promise(resolve => {
  22. // PromisuQueue
  23. console.log('// PromisuQueue');
  24. const promisuQueue = PromisuQueue.of({ concurrency: 1 });
  25. promisuQueue.add(asyncTask(1000), { priority: 1 }).then(() => {
  26. console.log('async task 1000 Done');
  27. });
  28. promisuQueue.addAll([asyncTask(2000), asyncTask(4000)], { priority: 2 }).then(() => {
  29. console.log('async task 2000/4000 Done');
  30. resolve()
  31. });
  32. promisuQueue.add(asyncTask(3000), { priority: 3 }).then(() => {
  33. console.log('async task 3000 Done');
  34. });
  35. // async task 1000 Done
  36. // async task 3000 Done
  37. // async task 2000/4000 Done
  38. })
  39. const testPromisuAll = () => new Promise(resolve => {
  40. // PromisuAll
  41. console.log('// PromisuAll');
  42. PromisuAll([asyncTask(2000), asyncTask(4000)], { concurrency: 2 })
  43. .then((result) => {
  44. console.log('PromisuAll done', result);
  45. resolve()
  46. })
  47. // PromisuAll done [ 2000, 4000 ]
  48. })
  49. const testPromisuRace = () => new Promise(resolve => {
  50. // PromisuRace
  51. console.log('// PromisuRace');
  52. PromisuRace([asyncTask(2000)(), asyncTask(1000)()])
  53. .then((result) => {
  54. console.log('PromisuRace done', result);
  55. resolve()
  56. })
  57. // PromisuRace done 1000
  58. })
  59. const testPromisuEvery = () => new Promise(resolve => {
  60. // PromisuEvery
  61. console.log('// PromisuEvery');
  62. const testFn = x => x > 1000;
  63. PromisuEvery([asyncTask(2000)(), 3000], testFn)
  64. .then((result) => {
  65. console.log('PromisuEvery done', result);
  66. resolve()
  67. })
  68. // PromisuEvery done true
  69. })
  70. const testPromisuSome = () => new Promise(resolve => {
  71. // PromisuSome
  72. console.log('// PromisuSome');
  73. const testFn = x => x > 2000;
  74. PromisuSome([asyncTask(2000)(), 3000], testFn)
  75. .then((result) => {
  76. console.log('PromisuSome done', result);
  77. resolve()
  78. })
  79. // PromisuSome done true
  80. })
  81. const testPromisuFew = () => new Promise(resolve => {
  82. // PromisuFew
  83. console.log('// PromisuFew');
  84. PromisuFew([asyncTask(2000)(), 3000, asyncTask(1000)()], { count: 2 })
  85. .then((result) => {
  86. console.log('PromisuFew done', result);
  87. resolve()
  88. })
  89. // PromisuFew done [ 3000, 1000 ]
  90. })
  91. const testPromisuMap = () => new Promise(resolve => {
  92. // PromisuMap
  93. console.log('// PromisuMap');
  94. const mapFn = x => new Promise(resolve => { resolve(x + 1) })
  95. PromisuMap([asyncTask(2000)(), 3000, asyncTask(1000)()], mapFn, { concurrency: 2 })
  96. .then((result) => {
  97. console.log('PromisuMap done', result);
  98. resolve()
  99. })
  100. // PromisuMap done [ 2001, 3001, 1001 ]
  101. })
  102. const testPromisuFilter = () => new Promise(resolve => {
  103. // PromisuFilter
  104. console.log('// PromisuFilter');
  105. const filterFn = x => new Promise(resolve => { resolve(x > 1000) })
  106. PromisuFilter([asyncTask(2000)(), 3000, asyncTask(1000)()], filterFn, { concurrency: 2 })
  107. .then((result) => {
  108. console.log('PromisuFilter done', result);
  109. resolve()
  110. })
  111. // PromisuFilter done [ 2000, 3000 ]
  112. })
  113. const testPromisuReduce = () => new Promise(resolve => {
  114. // PromisuReduce
  115. console.log('// PromisuReduce');
  116. const reduceFn = (acc, curr) => new Promise(resolve => { resolve(acc + curr) })
  117. PromisuReduce([asyncTask(2000)(), 3000, asyncTask(1000)()], reduceFn, 0)
  118. .then((result) => {
  119. console.log('PromisuReduce done', result);
  120. resolve()
  121. })
  122. // PromisuReduce done 6000
  123. })
  124. const testPromisuScan = () => new Promise(resolve => {
  125. // PromisuScan
  126. console.log('// PromisuScan');
  127. const scanFn = (acc, curr) => new Promise(resolve => { resolve(acc + curr) })
  128. PromisuScan([asyncTask(2000)(), 3000, asyncTask(1000)()], scanFn, 0)
  129. .then((result) => {
  130. console.log('PromisuScan done', result);
  131. resolve()
  132. })
  133. // PromisuScan done [ 0, 2000, 5000, 6000 ]
  134. })
  135. const testPromisuWaitFor = () => new Promise(resolve => {
  136. // PromisuWaitFor
  137. console.log('// PromisuWaitFor');
  138. const waitFn = x => () => new Promise(resolve => { resolve(x) })
  139. PromisuWaitFor(waitFn(true), 200)
  140. .then(() => {
  141. console.log('PromisuWaitFor done');
  142. })
  143. PromisuWaitFor(waitFn(false), 200)
  144. .catch(() => {
  145. // console.log('PromisuWaitFor done');
  146. resolve()
  147. })
  148. // PromisuWaitFor done
  149. })
  150. const testPromisuFinally = () => new Promise(resolve => {
  151. // PromisuFinally
  152. console.log('// PromisuFinally');
  153. // TODO
  154. resolve()
  155. })
  156. const testPromisuTry = () => new Promise(resolve => {
  157. // PromisuTry
  158. console.log('// PromisuTry');
  159. // TODO
  160. resolve()
  161. })
  162. const testPromisuDebounce = () => new Promise(resolve => {
  163. // PromisuDebounce
  164. console.log('// PromisuDebounce');
  165. // TODO
  166. resolve()
  167. })
  168. const testPromisuThrottle = () => new Promise(resolve => {
  169. // PromisuThrottle
  170. console.log('// PromisuThrottle');
  171. // TODO
  172. resolve()
  173. })
  174. const testArr = [
  175. testPromisuQueue,
  176. testPromisuAll,
  177. testPromisuRace,
  178. testPromisuEvery,
  179. testPromisuSome,
  180. testPromisuFew,
  181. testPromisuMap,
  182. testPromisuFilter,
  183. testPromisuReduce,
  184. testPromisuScan,
  185. testPromisuWaitFor,
  186. testPromisuFinally,
  187. testPromisuTry,
  188. testPromisuDebounce,
  189. testPromisuThrottle
  190. ]
  191. const run = async (testArr) => {
  192. for (let next of testArr) {
  193. await next()
  194. }
  195. }
  196. run(testArr)

Installation

  1. npm install --save promisu

Usage

You can import one or multiple operators from promisu:

  1. import {
  2. PromisuAll,
  3. PromisuMap,
  4. PromisuEvery,
  5. PromisuFew,
  6. PromisuFilter,
  7. PromisuFinally,
  8. PromisuQueue,
  9. PromisuRace,
  10. PromisuReduce,
  11. PromisuSome,
  12. PromisuTry,
  13. PromisuWaitFor,
  14. PromisuScan,
  15. PromisuDebounce,
  16. PromisuThrottle
  17. } from 'promisu';
  18. // or
  19. const {
  20. PromisuAll,
  21. PromisuMap,
  22. PromisuEvery,
  23. PromisuFew,
  24. PromisuFilter,
  25. PromisuFinally,
  26. PromisuQueue,
  27. PromisuRace,
  28. PromisuReduce,
  29. PromisuSome,
  30. PromisuTry,
  31. PromisuWaitFor,
  32. PromisuScan,
  33. PromisuDebounce,
  34. PromisuThrottle
  35. } = require('promisu');