Skip to content

Instantly share code, notes, and snippets.

@danielstjules
Last active March 30, 2019 01:19
Show Gist options
  • Save danielstjules/a93df1cd34fb9f0b7041 to your computer and use it in GitHub Desktop.
Save danielstjules/a93df1cd34fb9f0b7041 to your computer and use it in GitHub Desktop.
// 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