Guest User

Untitled

a guest
Jan 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. import fs from 'fs-promise'
  2.  
  3. async function printFiles () {
  4. const files = await getFilePaths() // Assume this works fine
  5.  
  6. files.forEach(async (file) => {
  7. const contents = await fs.readFile(file, 'utf8')
  8. console.log(contents)
  9. })
  10. }
  11.  
  12. printFiles()
  13.  
  14. async function printFiles () {
  15. const files = await getFilePaths();
  16.  
  17. for (const file of files) {
  18. const contents = await fs.readFile(file, 'utf8');
  19. console.log(contents);
  20. }
  21. }
  22.  
  23. async function printFiles () {
  24. const files = await getFilePaths();
  25.  
  26. await Promise.all(files.map(async (file) => {
  27. const contents = await fs.readFile(file, 'utf8')
  28. console.log(contents)
  29. }));
  30. }
  31.  
  32. async function printFiles () {
  33. const files = await getFilePaths()
  34.  
  35. for await (const file of fs.readFile(file, 'utf8')) {
  36. console.log(contents)
  37. }
  38. }
  39.  
  40. const { forEach } = require('p-iteration');
  41. const fs = require('fs-promise');
  42.  
  43. async function printFiles () {
  44. const files = await getFilePaths();
  45.  
  46. await forEach(files, async (file) => {
  47. const contents = await fs.readFile(file, 'utf8');
  48. console.log(contents);
  49. });
  50. }
  51.  
  52. printFiles()
  53.  
  54. Array.prototype.forEachAsync = async function (fn) {
  55. for (let t of this) { await fn(t) }
  56. }
  57.  
  58. Array.prototype.forEachAsyncParallel = async function (fn) {
  59. await Promise.all(this.map(fn));
  60. }
  61.  
  62. Promise.all(PacksList.map((pack)=>{
  63. return fireBaseRef.child(pack.folderPath).once('value',(snap)=>{
  64. snap.forEach( childSnap => {
  65. const file = childSnap.val()
  66. file.id = childSnap.key;
  67. allItems.push( file )
  68. })
  69. })
  70. })).then(()=>store.dispatch( actions.allMockupItems(allItems)))
  71.  
  72. module.exports = function () {
  73. var self = this;
  74.  
  75. this.each = async (items, fn) => {
  76. if (items && items.length) {
  77. await Promise.all(
  78. items.map(async (item) => {
  79. await fn(item);
  80. }));
  81. }
  82. };
  83.  
  84. this.reduce = async (items, fn, initialValue) => {
  85. await self.each(
  86. items, async (item) => {
  87. initialValue = await fn(initialValue, item);
  88. });
  89. return initialValue;
  90. };
  91. };
  92.  
  93. ...
  94. /* your server setup here */
  95. ...
  96. var MyAsync = require('./myAsync');
  97. var Cat = require('./models/Cat');
  98. var Doje = require('./models/Doje');
  99. var example = async () => {
  100. var myAsync = new MyAsync();
  101. var doje = await Doje.findOne({ name: 'Doje', noises: [] }).save();
  102. var cleanParams = [];
  103.  
  104. // FOR EACH EXAMPLE
  105. await myAsync.each(['bork', 'concern', 'heck'],
  106. async (elem) => {
  107. if (elem !== 'heck') {
  108. await doje.update({ $push: { 'noises': elem }});
  109. }
  110. });
  111.  
  112. var cat = await Cat.findOne({ name: 'Nyan' });
  113.  
  114. // REDUCE EXAMPLE
  115. var friendsOfNyanCat = await myAsync.reduce(cat.friends,
  116. async (catArray, friendId) => {
  117. var friend = await Friend.findById(friendId);
  118. if (friend.name !== 'Long cat') {
  119. catArray.push(friend.name);
  120. }
  121. }, []);
  122. // Assuming Long Cat was a friend of Nyan Cat...
  123. assert(friendsOfNyanCat.length === (cat.friends.length - 1));
  124. }
  125.  
  126. async function printFiles() {
  127. const files = await getFiles();
  128.  
  129. List(files).traverse( Task.of, f => readFile( f, 'utf-8'))
  130. .fork( console.error, console.log)
  131. }
  132.  
  133. import fs from 'fs';
  134. import { futurize } from 'futurize';
  135. import Task from 'data.task';
  136. import { List } from 'immutable-ext';
  137.  
  138. const future = futurizeP(Task)
  139. const readFile = future(fs.readFile)
  140.  
  141. const printFiles = files =>
  142. List(files).traverse( Task.of, fn => readFile( fn, 'utf-8'))
  143. .fork( console.error, console.log)
  144.  
  145. // 90% of encodings are utf-8, making that use case super easy is prudent
  146.  
  147. // handy-library.js
  148. export const readFile = f =>
  149. future(fs.readFile)( f, 'utf-8' )
  150.  
  151. export const arrayToTaskList = list => taskFn =>
  152. List(files).traverse( Task.of, taskFn )
  153.  
  154. export const readFiles = files =>
  155. arrayToTaskList( files, readFile )
  156.  
  157. export const printFiles = files =>
  158. readFiles(files).fork( console.error, console.log)
  159.  
  160. async function main() {
  161. /* awesome code with side-effects before */
  162. printFiles( await getFiles() );
  163. /* awesome code with side-effects after */
  164. }
  165.  
  166. import { curry, flip } from 'ramda'
  167.  
  168. export const readFile = fs.readFile
  169. |> future,
  170. |> curry,
  171. |> flip
  172.  
  173. export const readFileUtf8 = readFile('utf-8')
  174.  
  175. import fs from 'fs-promise';
  176. async function printFiles () {
  177. const files = await getFilePaths();
  178.  
  179. const promises = files.map((file) => fs.readFile(file, 'utf8'))
  180.  
  181. const contents = await Promise.all(promises)
  182.  
  183. contents.forEach(console.log);
  184. }
  185.  
  186. files.reduce((lastPromise, file) =>
  187. lastPromise.then(() =>
  188. fs.readFile(file, 'utf8')
  189. ), Promise.resolve()
  190. )
  191.  
  192. Array.prototype.forEachAsync = async function(cb){
  193. for(let x of this){
  194. await cb(x);
  195. }
  196. }
  197.  
  198. const AsyncAF = require('async-af');
  199. const fs = require('fs-promise');
  200.  
  201. function printFiles() {
  202. // since AsyncAF accepts promises or non-promises, there's no need to await here
  203. const files = getFilePaths();
  204.  
  205. AsyncAF(files).forEach(async file => {
  206. const contents = await fs.readFile(file, 'utf8');
  207. console.log(contents);
  208. });
  209. }
  210.  
  211. printFiles();
  212.  
  213. const AsyncAF = require('async-af');
  214. const fs = require('fs-promise');
  215.  
  216. function printFiles() {
  217. const files = getFilePaths();
  218.  
  219. AsyncAF(files).forEach(file => {
  220. AsyncAF.log(fs.readFile(file, 'utf8'));
  221. });
  222. }
  223.  
  224. printFiles();
  225.  
  226. const aaf = require('async-af');
  227. const fs = require('fs-promise');
  228.  
  229. const printFiles = () => aaf(getFilePaths())
  230. .map(file => fs.readFile(file, 'utf8'))
  231. .forEach(file => aaf.log(file));
  232.  
  233. printFiles();
Add Comment
Please, Sign In to add comment