Guest User

Untitled

a guest
Jul 25th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. var fs = require('fs')
  2. var Q = require('q')
  3. var fs_stat = Q.denodeify(fs.stat)
  4. var fs_readdir = Q.denodeify(fs.readdir)
  5.  
  6. var files = [
  7. './fixtures/file1',
  8. './fixtures/file2',
  9. './fixtures/file3',
  10. './fixtures/file4'
  11. ]
  12.  
  13. var dirs = [
  14. '.',
  15. '../',
  16. '../../'
  17. ]
  18.  
  19. // each, eachSeries, and eachLimit are very similar to their map counterparts, only the results are ignored
  20. // order is always preserved, regardless if done in parallel or series
  21.  
  22. function map (arr, func) {
  23. return Q().then(function () {
  24. return arr.map(function (el) { return func(el) })
  25. }).all()
  26. }
  27.  
  28. function mapSeries (arr, func) {
  29. var currentPromise = Q()
  30. var promises = arr.map(function (el) {
  31. return currentPromise = currentPromise.then(function () {
  32. return func(el)
  33. })
  34. })
  35. return Q.all(promises)
  36. }
  37.  
  38. function mapLimit (arr, limit, func) {
  39. var batches = arr.reduce(function (last, next, index) {
  40. if (index % limit == 0) last.push([next])
  41. else last[last.length-1].push(next)
  42. return last
  43. }, [])
  44.  
  45. var currentPromise = Q()
  46. var promises = batches.map(function (batch) {
  47. return currentPromise = currentPromise.then(function () {
  48. var batchPromises = batch.map(function (el) { return func(el) })
  49. return Q.all(batchPromises)
  50. })
  51. })
  52.  
  53. return Q.all(promises)
  54. .then(function (results) {
  55. return Array.prototype.concat.apply([], results) // flatten array
  56. })
  57. }
  58.  
  59. // map(files, fs_stat).then(console.log, console.error)
  60. // function word (file) { throw 'oh no dawg'; return 'word to this file: ' + file }
  61. // map(files, word).then(console.log, function (er) { console.error('oh no', er) })
  62. // mapSeries(files, fs_stat).then(console.log, console.error)
  63. // mapLimit(files, 2, fs_stat).then(console.log, console.error)
  64.  
  65. function filter (arr, func) {
  66. var promises = arr.map(function (el) { return func(el) })
  67. return Q.all(promises).then(function (results) {
  68. return arr.filter(function (el,i) { return results[i] })
  69. })
  70. }
  71.  
  72. function filterSeries (arr, func) {
  73. var currentPromise = Q()
  74. var promises = arr.map(function (el) {
  75. return currentPromise = currentPromise.then(function () {
  76. return func(el)
  77. })
  78. })
  79. return Q.all(promises).then(function (results) {
  80. return arr.filter(function (el,i) { return results[i] })
  81. })
  82. }
  83.  
  84. // filter(files, fs_stat).then(console.log, console.error)
  85. // filterSeries(files, fs_stat).then(console.log, console.error)
  86.  
  87. function reject (arr, func) {
  88. var promises = arr.map(function (el) { return func(el) })
  89. return Q.all(promises).then(function (results) {
  90. return arr.filter(function (el,i) { return !results[i] })
  91. })
  92. }
  93.  
  94. function rejectSeries (arr, func) {
  95. var currentPromise = Q()
  96. var promises = arr.map(function (el) {
  97. return currentPromise = currentPromise.then(function () {
  98. return func(el)
  99. })
  100. })
  101. return Q.all(promises).then(function (results) {
  102. return arr.filter(function (el,i) { return !results[i] })
  103. })
  104. }
  105.  
  106. // reject(files, fs_stat).then(console.log, console.error)
  107. // rejectSeries(files, fs_stat).then(console.log, console.error)
  108.  
  109. function reduce (arr, initialVal, func) {
  110. var currentPromise = Q(initialVal)
  111. arr.map(function (el) {
  112. return currentPromise = currentPromise.then(function (memo) {
  113. return func(memo, el)
  114. })
  115. })
  116. return currentPromise
  117. }
  118.  
  119. function reduceRight (arr, initialVal, func) {
  120. var currentPromise = Q(initialVal)
  121. arr.reverse().map(function (el) {
  122. return currentPromise = currentPromise.then(function (memo) {
  123. return func(memo, el)
  124. })
  125. })
  126. return currentPromise
  127. }
  128.  
  129. // reduce([1,2,3], 0, function (memo, item) { return Q.delay(100).thenResolve(memo + item) }).then(console.log, console.error)
  130. // reduceRight([1,2,3], 0, function (memo, item) { return Q.delay(100).thenResolve(memo + item) }).then(console.log, console.error)
  131.  
  132. function detect (arr, func) {
  133. var promises = arr.map(function (el) { return func(el) })
  134. return Q.all(promises).then(function (results) {
  135. return arr.filter(function (el,i) { return results[i] }).shift()
  136. })
  137. }
  138.  
  139. function detectSeries (arr, func) {
  140. var currentPromise = Q()
  141. var promises = arr.map(function (el) {
  142. return currentPromise = currentPromise.then(function () {
  143. return func(el)
  144. })
  145. })
  146. return Q.all(promises).then(function (results) {
  147. return arr.filter(function (el,i) { return results[i] }).shift() // could be optimized to return first
  148. })
  149. }
  150.  
  151. // detect(files, fs_stat).then(console.log, console.error)
  152. // detectSeries(files, fs_stat).then(console.log, console.error)
  153.  
  154. function sortBy (arr, func) {
  155. var promises = arr.map(function (el) { return func(el) })
  156. return Q.all(promises).then(function (results) {
  157. return arr.sort(function (a, b) {
  158. return results[arr.indexOf(a)] < results[arr.indexOf(b)] ? -1 : 1
  159. })
  160. })
  161. }
  162.  
  163. // sortBy([2,1,3], function (item) { return Q.delay(100).thenResolve(item) }).then(console.log, console.error)
  164.  
  165. function some (arr, func) {
  166. var promises = arr.map(function (el) { return func(el) })
  167. return Q.all(promises).then(function (results) {
  168. return results.some(function (el) { return el })
  169. })
  170. }
  171.  
  172. // some([2,1,3], function (item) { return Q.delay(100).thenResolve(item == 1) }).then(console.log, console.error)
  173.  
  174. function every (arr, func) {
  175. var promises = arr.map(function (el) { return func(el) })
  176. return Q.all(promises).then(function (results) {
  177. return results.every(function (el) { return el })
  178. })
  179. }
  180.  
  181. // every([1,1,1], function (item) { return Q.delay(100).thenResolve(item == 1) }).then(console.log, console.error)
  182.  
  183. function concat (arr, func) {
  184. var promises = arr.map(function (el) { return func(el) })
  185. return Q.all(promises).then(function (results) {
  186. return Array.prototype.concat.apply([], results) // flatten results
  187. })
  188. }
  189.  
  190. function concatSeries (arr, func) {
  191. var currentPromise = Q()
  192. var promises = arr.map(function (el) {
  193. return currentPromise = currentPromise.then(function () {
  194. return func(el)
  195. })
  196. })
  197. return Q.all(promises).then(function (results) {
  198. return Array.prototype.concat.apply([], results) // flatten results
  199. })
  200. }
  201.  
  202. // concat(dirs, fs_readdir).then(console.log, console.error)
  203. // concatSeries(dirs, fs_readdir).then(console.log, console.error)
  204.  
  205. function parallel (funcs) {
  206. var promises = funcs.map(function (func) { return func() })
  207. return Q.all(promises)
  208. }
  209.  
  210. function series (funcs) {
  211. var currentPromise = Q()
  212. var promises = funcs.map(function (func) {
  213. return currentPromise = currentPromise.then(func)
  214. })
  215. return Q.all(promises)
  216. }
  217.  
  218. function parallelLimit (funcs) {
  219. var batches = funcs.reduce(function (last, next, index) {
  220. if (index % limit == 0) last.push([next])
  221. else last[last.length-1].push(next)
  222. return last
  223. }, [])
  224.  
  225. var currentPromise = Q()
  226. var promises = batches.map(function (batch) {
  227. return currentPromise = currentPromise.then(function () {
  228. return Q.all(batch)
  229. })
  230. })
  231.  
  232. return Q.all(promises)
  233. .then(function (results) {
  234. return Array.prototype.concat.apply([], results) // flatten array
  235. })
  236. }
  237.  
  238. function wilst (test, func) {
  239. if (!test()) return Q('wilst')
  240. return func().then(function () {
  241. return wilst(test, func)
  242. })
  243. }
  244.  
  245. function doWilst (func, test) {
  246. return func().then(function () {
  247. if (!test()) return Q('dowilst')
  248. return doWilst(func, test)
  249. })
  250. }
  251.  
  252. // var count = 0
  253. // wilst(function () { return count < 5 }, function () { count++; return Q.delay(100) }).then(console.log, console.error)
  254. // var doCount = 0
  255. // doWilst(function () { doCount++; return Q.delay(100) }, function () { return doCount < 5 }).then(console.log, console.error)
  256.  
  257. function until (test, func) {
  258. if (test()) return Q('until')
  259. return func().then(function () {
  260. return wilst(test, func)
  261. })
  262. }
  263.  
  264. function doUntil (func, test) {
  265. return func().then(function () {
  266. if (test()) return Q('dountil')
  267. return doUntil(func, test)
  268. })
  269. }
  270.  
  271. // var acount = 0
  272. // until(function () { return count > 5 }, function () { acount++; return Q.delay(100) }).then(console.log, console.error)
  273. // var adoCount = 0
  274. // doUntil(function () { adoCount++; return Q.delay(100) }, function () { return adoCount < 5 }).then(console.log, console.error)
  275.  
  276. function forever (func) {
  277. return func().then(function () { return forever(func) })
  278. }
  279.  
  280. // var trigger = 0
  281. // forever(function () {
  282. // if (trigger > 10000) throw "oh no"
  283. // return Q.delay(1)
  284. // }).then(console.log, console.error)
  285.  
  286. function waterfall (funcs) {
  287. return funcs.reduce(Q.when, Q())
  288. }
  289.  
  290. // waterfall([ function () { return fs_stat('./fixtures/file1') }, function (stat) { console.log(stat) } ]).then(console.log, console.error)
  291.  
  292. function compose () {
  293. var funcs = Array.prototype.slice.call(arguments)
  294. return function () {
  295. return funcs.reduce(Q.when, Q.apply(null, arguments))
  296. }
  297. }
  298.  
  299. // function add1 (n) { return Q.delay(1000).thenResolve(n+1) }
  300. // function mul3 (n) { return Q.delay(1000).thenResolve(n*3) }
  301. // var add1mul3 = compose(add1, mul3)
  302. // add1mul3(20).then(console.log, console.error)
  303.  
  304. function applyEach (funcs) {
  305. var args = Array.prototype.slice.call(arguments, 1)
  306. var promises = funcs.map(function (func) { return Q.apply(Q, args).then(func) })
  307. return Q.all(promises)
  308. }
  309.  
  310. function applyEachSeries (funcs) {
  311. var args = Array.prototype.slice.call(arguments, 1)
  312. var currentPromise = Q()
  313. var promises = funcs.map(function (func) {
  314. return currentPromise = currentPromise.then(function () {
  315. return Q.apply(Q, args).then(func)
  316. })
  317. })
  318. return Q.all(promises)
  319. }
  320.  
  321. // function add2 (n) { return Q.delay(1000).thenResolve(n+2) }
  322. // function mul4 (n) { return Q.delay(1000).thenResolve(n*4) }
  323. // applyEach([ add2, mul4 ], 34).then(console.log, console.error)
  324. // applyEachSeries([ add2, mul4 ], 34).then(console.log, console.error)
Add Comment
Please, Sign In to add comment