Skip to content

Instantly share code, notes, and snippets.

@ryansukale
Last active August 29, 2015 14:04
Show Gist options
  • Save ryansukale/72b22a9be8a2ea09a092 to your computer and use it in GitHub Desktop.
Save ryansukale/72b22a9be8a2ea09a092 to your computer and use it in GitHub Desktop.
var getData = new Promise(function(resolve, reject){
// Do something asynchronous and pass it a callback
getDataFromAMillionMilesAway(function(data){
if(data){
resolve(data);
}
else {
reject(new Error("Thats too far away"));
}
});
});
getData().then(function(data){
console.log('Got Some Data! ', data);
});
getData().then(
function(data){
console.log('Got Some Data! ', data);
},
function(error){
console.log('Something really bad happened :( ', data);
}
);
var getData = function(config){
return new Promise(function(resolve, reject){
// Do something asynchronous and pass it a callback
getDataFromAMillionMilesAway(config, function(data){
if(data){
resolve(data);
}
else {
reject(new Error("Thats too far away"));
}
});
});
}
getData(config1)
.then(getData(config2))
.then(getData(config3))
.then(function(){
console.log('All the data was fetched sequentially.')
});
getData(config).then(function(data){
data = _.extend({}, data, {'key','valye'});
return data;
}).then(function(modifiedData){
console.log(modifiedData);
});
getData(config)
.then(function(data){
return JSON.parse(data);
})
.then(function(parsedData){
console.log(parsedData);
});
is equivalent too
getData(config)
.then(JSON.parse)
.then(function(parsedData){
console.log(parsedData);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment