Last active
April 24, 2016 09:40
-
-
Save msteckyefantis/73ef280dbc99c395c2a238c31bfa5b00 to your computer and use it in GitHub Desktop.
an example promise function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
To run this file: | |
1. download this code or copy this code and put it in a file, with .js at the end of the filename | |
2. in your command line go to the directory where the code is | |
3. input in your command line: node Promise.js (or whatever your filename is called) | |
4. SEE THE SECRET MESSAGE!! | |
*/ | |
function myInnerPromise( value ) { | |
return Promise.resolve( value + ' is ' ); | |
} | |
function myInnerInnerPromise( value ) { | |
return Promise.resolve( value + 'very cool.' ); | |
} | |
/* | |
* @Promise | |
*/ | |
function myPromise( x , y ) { | |
if( !x || !y ) { | |
return Promise.resolve( false ); | |
} | |
// myInnerPromise returns the value z to the next inner 'then' | |
return myInnerPromise( x + y ) | |
.then( function( z ) { | |
// myInnerInnerPromise retuns w to the next inner 'then' | |
return myInnerInnerPromise( z ) | |
.then( function( w ) { | |
/* | |
this w is returned to the next outer 'then' | |
because there are no more inner 'then's | |
*/ | |
return w; | |
}); | |
}) | |
.then( function( w ) { | |
console.log( 'Final result:', w ); | |
}) | |
.catch( function( err ) { | |
// this catch will catch any error that occurs within myInnerPromise | |
console.log( 'Error:', err.message ); | |
}); | |
} | |
myPromise( 'Taylor' , ' Swift' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment