Skip to content

Instantly share code, notes, and snippets.

@mlent
Created September 8, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlent/d23cbd5b3f5935229933acd82263321a to your computer and use it in GitHub Desktop.
Save mlent/d23cbd5b3f5935229933acd82263321a to your computer and use it in GitHub Desktop.
function sendDataToAmazon(dataToSend) {
return getAmazonKey()
.then(getUrlWithKey)
.then(function(url) {
/* Sometimes it's easier to inline if you have
data passed into original function which is only
needed in the middle of a promise chain*/
return sendDataToUrl(url, dataToSend);
})
.then(updateUI);
}
function getAmazonKey() {
return $.get('/amazon/keys').then(function(data) {
return { key: data.key, ;
}
}
function getUrlWithKey(key) {
return $.get('/amazon/urls?key=' + key).then(function(data) {
return data.url;
});
}
}
function sendDataToUrl(url, dataToSend) {
return $.ajax({
method: 'POST',
url: url,
data: dataToSend,
encoding: 'whatever-files-types?'
});
}
function updateUI(data) {
$('#my-selector').html('It worked!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment