22
33// Emulate asynchronous calls
44
5- const wrapAsync = callback => setTimeout (
5+ const wrapAsync = ( callback ) => setTimeout (
66 callback , Math . floor ( Math . random ( ) * 1000 )
77) ;
88
99const isWeekend = ( ) => ! ( new Date ( ) . getDay ( ) % 6 ) ;
1010
1111// Asynchronous functions
1212
13- const readConfig = name => new Promise ( ( resolve , reject ) => {
13+ const readConfig = ( name ) => new Promise ( ( resolve , reject ) => {
1414 wrapAsync ( ( ) => {
1515 console . log ( '(1) config loaded' ) ;
1616 if ( ! isWeekend ( ) ) resolve ( { name } ) ;
1717 else reject ( new Error ( 'Promises will resolve next working day' ) ) ;
1818 } ) ;
1919} ) ;
2020
21- const doQuery = statement => new Promise ( ( resolve , reject ) => {
21+ const doQuery = ( statement ) => new Promise ( ( resolve , reject ) => {
2222 wrapAsync ( ( ) => {
2323 console . log ( '(2) SQL query executed: ' + statement ) ;
2424 if ( ! isWeekend ( ) ) resolve ( [ { name : 'Kiev' } , { name : 'Roma' } ] ) ;
2525 else reject ( new Error ( 'Promises will resolve next working day' ) ) ;
2626 } ) ;
2727} ) ;
2828
29- const httpGet = url => new Promise ( ( resolve , reject ) => {
29+ const httpGet = ( url ) => new Promise ( ( resolve , reject ) => {
3030 wrapAsync ( ( ) => {
3131 console . log ( '(3) Page retrieved: ' + url ) ;
3232 if ( ! isWeekend ( ) ) resolve ( '<html>Some archaic web here</html>' ) ;
3333 else reject ( new Error ( 'Promises will resolve next working day' ) ) ;
3434 } ) ;
3535} ) ;
3636
37- const readFile = path => new Promise ( ( resolve , reject ) => {
37+ const readFile = ( path ) => new Promise ( ( resolve , reject ) => {
3838 wrapAsync ( ( ) => {
3939 console . log ( '(4) Readme file loaded: ' + path ) ;
4040 if ( ! isWeekend ( ) ) resolve ( 'file content' ) ;
@@ -48,10 +48,10 @@ Promise.resolve()
4848 . then ( readConfig . bind ( null , 'myConfig' ) )
4949 . then ( doQuery . bind ( null , 'select * from cities' ) )
5050 . then ( httpGet . bind ( null , 'http://kpi.ua' ) )
51- . catch ( err => console . log ( 'Reject reason (1): ' + err . message ) )
51+ . catch ( ( err ) => console . log ( 'Reject reason (1): ' + err . message ) )
5252 . then ( readFile . bind ( null , 'README.md' ) )
53- . catch ( err => console . log ( 'Reject reason (2): ' + err . message ) )
54- . then ( data => {
53+ . catch ( ( err ) => console . log ( 'Reject reason (2): ' + err . message ) )
54+ . then ( ( data ) => {
5555 console . log ( 'Done' ) ;
5656 console . dir ( { data } ) ;
5757 } ) ;
0 commit comments