1+ 'use strict' ;
2+
13global . api = { } ;
24api . fs = require ( 'fs' ) ;
35
46let reduceAsync = function ( items , performer , callback , initialValue ) {
5- var nseted = ( typeof initialValue === 'undefined' ) ,
6- counter = nseted ? 1 : 0 ,
7- previous = nseted ? items [ 0 ] : initialValue ,
8- current = nseted ? items [ 1 ] : items [ 0 ] ;
7+ var nseted = ( typeof ( initialValue ) === 'undefined' ) ,
8+ counter = nseted ? 1 : 0 ,
9+ previous = nseted ? items [ 0 ] : initialValue ,
10+ current = nseted ? items [ 1 ] : items [ 0 ] ;
911
1012 function response ( err , data ) {
1113 if ( ! err && counter !== items . length - 1 ) {
1214 ++ counter ;
1315 previous = data ;
1416 current = items [ counter ] ;
1517 performer ( previous , current , response , counter , items ) ;
16- }
17- else {
18- if ( callback ) callback ( err , data ) ;
18+ } else if ( callback ) {
19+ callback ( err , data ) ;
1920 }
2021 }
2122
2223 performer ( previous , current , response , counter , items ) ;
23- }
24+ } ;
2425
2526// params - array of parametrs for functions
2627// args - array of functions
2728// args[i] - function
2829// args[-1] - callback(err, data)
30+ //
2931function composeAsync ( params , ...args ) {
3032 reduceAsync (
3133 args . slice ( 0 , - 1 ) ,
32- ( params , fn , callback ) => fn . apply ( null , [ ] . concat ( params ) . concat ( callback ) ) ,
34+ ( params , fn , callback ) => fn . apply (
35+ null , [ ] . concat ( params ) . concat ( callback )
36+ ) ,
3337 args [ args . length - 1 ] ,
3438 params
3539 ) ;
@@ -50,20 +54,20 @@ function wrapAsync(callback) {
5054}
5155
5256function read ( file , charset , callback ) {
53- console . dir ( { read :{ file, callback} } ) ;
57+ console . dir ( { read : { file, callback } } ) ;
5458 api . fs . readFile ( file , charset , callback ) ;
5559}
5660
5761function parse ( data , callback ) {
58- console . dir ( { parse :{ data, callback} } ) ;
59- wrapAsync ( ( ) => {
60- callback ( null , [ 'Data has been' , 'processed!' ] ) ;
61- } ) ;
62+ console . dir ( { parse : { data, callback } } ) ;
63+ wrapAsync ( ( ) => {
64+ callback ( null , [ 'Data has been' , 'processed!' ] ) ;
65+ } ) ;
6266}
6367
6468function preprocess ( data1 , data2 , callback ) {
65- console . dir ( { preprocess :{ data1, data2, callback} } ) ;
66- wrapAsync ( ( ) => {
67- callback ( null , data1 + ' ' + data2 ) ;
68- } ) ;
69+ console . dir ( { preprocess : { data1, data2, callback } } ) ;
70+ wrapAsync ( ( ) => {
71+ callback ( null , data1 + ' ' + data2 ) ;
72+ } ) ;
6973}
0 commit comments