Last active
March 30, 2019 01:19
-
-
Save danielstjules/a93df1cd34fb9f0b7041 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
// How do you format your promise-based code | |
// Example from https://github.com/zendesk/cross-storage | |
var storage = new CrossStorageClient('https://store.example.com/hub.html'); | |
var errorHandler = function(err) { | |
// do something | |
}; | |
// Style 1 | |
storage.onConnect().then(function() { | |
return storage.set('newKey', 'foobar', 90000); | |
}).then(function() { | |
return storage.get('existingKey', 'newKey'); | |
}).then(function(res) { | |
console.log(res.length); | |
}).catch(errorHandler); | |
// Style 2 | |
storage.onConnect() | |
.then(function() { | |
return storage.set('newKey', 'foobar', 90000); | |
}).then(function() { | |
return storage.get('existingKey', 'newKey'); | |
}).then(function(res) { | |
console.log(res.length); | |
}).catch(errorHandler); | |
// Style 3 | |
storage | |
.onConnect() | |
.then(function() { | |
return storage.set('newKey', 'foobar', 90000); | |
}).then(function() { | |
return storage.get('existingKey', 'newKey'); | |
}).then(function(res) { | |
console.log(res.length); | |
}).catch(errorHandler); | |
// Style 4 | |
storage | |
.onConnect() | |
.then(function() { | |
return storage.set('newKey', 'foobar', 90000); | |
}).then(function() { | |
return storage.get('existingKey', 'newKey'); | |
}).then(function(res) { | |
console.log(res.length); | |
}).catch(errorHandler); | |
// Style 5 | |
storage | |
.onConnect() | |
.then(function() { | |
return storage.set('newKey', 'foobar', 90000); | |
}) | |
.then(function() { | |
return storage.get('existingKey', 'newKey'); | |
}) | |
.then(function(res) { | |
console.log(res.length); | |
}) | |
.catch(errorHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment