Skip to content

Instantly share code, notes, and snippets.

@shidhincr
Created January 28, 2015 08:25
Show Gist options
  • Save shidhincr/ddbe70f3d2a743ec718c to your computer and use it in GitHub Desktop.
Save shidhincr/ddbe70f3d2a743ec718c to your computer and use it in GitHub Desktop.
Promises Chaining Example
/* --------------------------------
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