Created
January 28, 2015 08:25
-
-
Save shidhincr/ddbe70f3d2a743ec718c to your computer and use it in GitHub Desktop.
Promises Chaining Example
This file contains hidden or 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
/* -------------------------------- | |
get user id | |
get commit history | |
get first commit | |
get first file name | |
-------------------------------- */ | |
'use strict'; | |
function getUserId(text){ | |
return new Promise(function(resolve){ | |
setTimeout(function(){ | |
text+= 'For user = shidhin and '; | |
resolve(['shidhin', text]); | |
}, 1000); | |
}); | |
} | |
function getCommitHistory(args){ | |
var | |
userId = args[0], | |
text = args[1]; | |
return new Promise(function(resolve){ | |
setTimeout(function(){ | |
text+='for commit = adsfasd879897 and '; | |
resolve(['adsfasd879897', text]); | |
}, 1000); | |
}); | |
} | |
function getFirstCommit(args){ | |
var | |
commitHistory = args[0], | |
text = args[1]; | |
return new Promise(function(resolve){ | |
setTimeout(function(){ | |
text+= 'for firstCommit = '+ commitHistory.slice(3,12); | |
resolve([commitHistory.slice(3,12), text]); | |
}, 1000); | |
}); | |
} | |
function getFirstFilename(args){ | |
var | |
firstCommit = args[0], | |
text = args[1]; | |
return new Promise(function(resolve){ | |
setTimeout(function(){ | |
text+='for filename = fileName_'+firstCommit+'.txt'; | |
resolve(['fileName_'+firstCommit+'.txt', text]); | |
}, 1000); | |
}); | |
} | |
getUserId('Hey, ') | |
.then(getCommitHistory) | |
.then(getFirstCommit) | |
.then(getFirstFilename) | |
.then(function(args){ | |
console.log(args[0]); | |
console.log(args[1]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment