Last active
August 29, 2015 14:04
-
-
Save ryansukale/72b22a9be8a2ea09a092 to your computer and use it in GitHub Desktop.
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
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); | |
}); |
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
getData().then( | |
function(data){ | |
console.log('Got Some Data! ', data); | |
}, | |
function(error){ | |
console.log('Something really bad happened :( ', data); | |
} | |
); |
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
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")); | |
} | |
}); | |
}); | |
} |
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
getData(config1) | |
.then(getData(config2)) | |
.then(getData(config3)) | |
.then(function(){ | |
console.log('All the data was fetched sequentially.') | |
}); |
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
getData(config).then(function(data){ | |
data = _.extend({}, data, {'key','valye'}); | |
return data; | |
}).then(function(modifiedData){ | |
console.log(modifiedData); | |
}); |
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
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