Skip to content

Instantly share code, notes, and snippets.

@maybe-joe
Last active July 15, 2016 05:55
Show Gist options
  • Save maybe-joe/1adf6813e6061b8cd094d40d7a1440f2 to your computer and use it in GitHub Desktop.
Save maybe-joe/1adf6813e6061b8cd094d40d7a1440f2 to your computer and use it in GitHub Desktop.
Flatter abs in just 3 refactorings!
// Run $node flat.js to test
// Test data
let context = {
succeed: () => console.log('Context Succeed')
};
function authenticateToSalesforce () {
return new Promise((resolve, reject) => resolve({data: 'authorization data'}));
}
function readMessagesFromQueue() {
return new Promise((resolve, reject) => resolve({data: 'messages data'}));
}
function persistMessagesToSalesforce() {
return new Promise((resolve, reject) => resolve());
}
function removeMessagesFromQueue() {
return new Promise((resolve, reject) => resolve());
}
// Your Code
authenticateToSalesforce()
.then(readFromQueue)
.catch(onError);
function readFromQueue(authorization) {
console.log('Read From Queue');
readMessagesFromQueue()
.then(messages => persistInSalesforce(messages, authorization))
.catch(onError);
}
function persistInSalesforce(messages, authorization) {
console.log('Persist In Salesforce', authorization.data, messages.data);
persistMessagesToSalesforce(authorization.data, messages.data)
.then(() => removeFromQueue(messages.data))
.catch(onError);
}
function removeFromQueue(data) {
console.log('Remove From Queue', data);
removeMessagesFromQueue(data)
.then(context.succeed)
.catch(onError);
}
function onError(err) {
console.error('Error', err);
context.fail(err.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment