Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from 'fs-promise'
- async function printFiles () {
- const files = await getFilePaths() // Assume this works fine
- files.forEach(async (file) => {
- const contents = await fs.readFile(file, 'utf8')
- console.log(contents)
- })
- }
- printFiles()
- async function printFiles () {
- const files = await getFilePaths();
- for (const file of files) {
- const contents = await fs.readFile(file, 'utf8');
- console.log(contents);
- }
- }
- async function printFiles () {
- const files = await getFilePaths();
- await Promise.all(files.map(async (file) => {
- const contents = await fs.readFile(file, 'utf8')
- console.log(contents)
- }));
- }
- async function printFiles () {
- const files = await getFilePaths()
- for await (const file of fs.readFile(file, 'utf8')) {
- console.log(contents)
- }
- }
- const { forEach } = require('p-iteration');
- const fs = require('fs-promise');
- async function printFiles () {
- const files = await getFilePaths();
- await forEach(files, async (file) => {
- const contents = await fs.readFile(file, 'utf8');
- console.log(contents);
- });
- }
- printFiles()
- Array.prototype.forEachAsync = async function (fn) {
- for (let t of this) { await fn(t) }
- }
- Array.prototype.forEachAsyncParallel = async function (fn) {
- await Promise.all(this.map(fn));
- }
- Promise.all(PacksList.map((pack)=>{
- return fireBaseRef.child(pack.folderPath).once('value',(snap)=>{
- snap.forEach( childSnap => {
- const file = childSnap.val()
- file.id = childSnap.key;
- allItems.push( file )
- })
- })
- })).then(()=>store.dispatch( actions.allMockupItems(allItems)))
- module.exports = function () {
- var self = this;
- this.each = async (items, fn) => {
- if (items && items.length) {
- await Promise.all(
- items.map(async (item) => {
- await fn(item);
- }));
- }
- };
- this.reduce = async (items, fn, initialValue) => {
- await self.each(
- items, async (item) => {
- initialValue = await fn(initialValue, item);
- });
- return initialValue;
- };
- };
- ...
- /* your server setup here */
- ...
- var MyAsync = require('./myAsync');
- var Cat = require('./models/Cat');
- var Doje = require('./models/Doje');
- var example = async () => {
- var myAsync = new MyAsync();
- var doje = await Doje.findOne({ name: 'Doje', noises: [] }).save();
- var cleanParams = [];
- // FOR EACH EXAMPLE
- await myAsync.each(['bork', 'concern', 'heck'],
- async (elem) => {
- if (elem !== 'heck') {
- await doje.update({ $push: { 'noises': elem }});
- }
- });
- var cat = await Cat.findOne({ name: 'Nyan' });
- // REDUCE EXAMPLE
- var friendsOfNyanCat = await myAsync.reduce(cat.friends,
- async (catArray, friendId) => {
- var friend = await Friend.findById(friendId);
- if (friend.name !== 'Long cat') {
- catArray.push(friend.name);
- }
- }, []);
- // Assuming Long Cat was a friend of Nyan Cat...
- assert(friendsOfNyanCat.length === (cat.friends.length - 1));
- }
- async function printFiles() {
- const files = await getFiles();
- List(files).traverse( Task.of, f => readFile( f, 'utf-8'))
- .fork( console.error, console.log)
- }
- import fs from 'fs';
- import { futurize } from 'futurize';
- import Task from 'data.task';
- import { List } from 'immutable-ext';
- const future = futurizeP(Task)
- const readFile = future(fs.readFile)
- const printFiles = files =>
- List(files).traverse( Task.of, fn => readFile( fn, 'utf-8'))
- .fork( console.error, console.log)
- // 90% of encodings are utf-8, making that use case super easy is prudent
- // handy-library.js
- export const readFile = f =>
- future(fs.readFile)( f, 'utf-8' )
- export const arrayToTaskList = list => taskFn =>
- List(files).traverse( Task.of, taskFn )
- export const readFiles = files =>
- arrayToTaskList( files, readFile )
- export const printFiles = files =>
- readFiles(files).fork( console.error, console.log)
- async function main() {
- /* awesome code with side-effects before */
- printFiles( await getFiles() );
- /* awesome code with side-effects after */
- }
- import { curry, flip } from 'ramda'
- export const readFile = fs.readFile
- |> future,
- |> curry,
- |> flip
- export const readFileUtf8 = readFile('utf-8')
- import fs from 'fs-promise';
- async function printFiles () {
- const files = await getFilePaths();
- const promises = files.map((file) => fs.readFile(file, 'utf8'))
- const contents = await Promise.all(promises)
- contents.forEach(console.log);
- }
- files.reduce((lastPromise, file) =>
- lastPromise.then(() =>
- fs.readFile(file, 'utf8')
- ), Promise.resolve()
- )
- Array.prototype.forEachAsync = async function(cb){
- for(let x of this){
- await cb(x);
- }
- }
- const AsyncAF = require('async-af');
- const fs = require('fs-promise');
- function printFiles() {
- // since AsyncAF accepts promises or non-promises, there's no need to await here
- const files = getFilePaths();
- AsyncAF(files).forEach(async file => {
- const contents = await fs.readFile(file, 'utf8');
- console.log(contents);
- });
- }
- printFiles();
- const AsyncAF = require('async-af');
- const fs = require('fs-promise');
- function printFiles() {
- const files = getFilePaths();
- AsyncAF(files).forEach(file => {
- AsyncAF.log(fs.readFile(file, 'utf8'));
- });
- }
- printFiles();
- const aaf = require('async-af');
- const fs = require('fs-promise');
- const printFiles = () => aaf(getFilePaths())
- .map(file => fs.readFile(file, 'utf8'))
- .forEach(file => aaf.log(file));
- printFiles();
Add Comment
Please, Sign In to add comment