Last active
January 5, 2016 09:29
-
-
Save luislee818/74f143ad0962313e9b95 to your computer and use it in GitHub Desktop.
JavaScript with Promises examples
This file contains hidden or 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
Promise.resolve().then(function() { | |
console.log('starting'); | |
return 123; | |
}).then(function(val) { | |
console.log(val); | |
return val + 1; | |
}).then(function(val) { | |
console.log('starting second step...'); | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('finished second step'); | |
resolve(val + 1); | |
}, 500); | |
}); | |
}).then(function() { | |
console.log('done'); | |
}); | |
// starting | |
// 123 | |
// starting second step... | |
// finished second step | |
// done |
This file contains hidden or 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 loggedIn = false; | |
var login = function() { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
loggedIn = true; | |
console.log("logged in"); | |
resolve('success'); | |
}, 500); | |
}) | |
}; | |
var display = function() { | |
var promise = loggedIn ? Promise.resolve() : login(); | |
promise.then(function() { | |
console.log('here is your secret'); | |
}); | |
}; | |
display(); | |
setTimeout(display, 1000) |
This file contains hidden or 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 authenticator = { | |
promise: undefined, | |
login: function() { | |
if (this.promise === undefined) { | |
this.promise = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
console.log("logged in"); | |
resolve('success'); | |
}, 500); | |
}); | |
} else { | |
console.log('already logged in'); | |
} | |
return this.promise; | |
} | |
}; | |
var display = function() { | |
authenticator.login().then(function() { | |
console.log("here's your account"); | |
}); | |
}; | |
display(); | |
setTimeout(display, 1000); |
This file contains hidden or 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 _ = require('underscore'); | |
var accounts = ['Checking Account', 'Travel Rewards Card', 'Big Box Retail Card']; | |
var promises = _.map(accounts, function(account) { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log("checked " + account); | |
resolve(); | |
}, 1000) | |
}); | |
}); | |
Promise.all(promises).then(function() { | |
console.log("Checked all"); | |
}); |
This file contains hidden or 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 _ = require('underscore'); | |
var sequence = function(products) { | |
_.reduce(products, function(promise, product) { | |
return promise.then(function() { | |
return queryFn(product); | |
}) | |
}, Promise.resolve()); | |
}; | |
var queryFn = function(product) { | |
return new Promise(function(resolve, reject) { | |
console.log("querying " + product); | |
setTimeout(function() { | |
var result = product + " checked."; | |
console.log(result); | |
resolve(product + " checked."); | |
}, 1000); | |
}); | |
}; | |
var products = ["a", "b", "c"]; | |
sequence(products); |
This file contains hidden or 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 _ = require('underscore'); | |
var products = ['a', 'b', 'c']; | |
var queryFn = function(product) { | |
return new Promise(function(resolve) { | |
console.log('checking ' + product); | |
setTimeout(function() { | |
console.log('checked ' + product); | |
resolve(); | |
}, 500); | |
}); | |
}; | |
var sequence = function(array) { | |
var chain; | |
chain = function(array, index) { | |
if (array.length === index) { | |
return Promise.resolve(); | |
} else { | |
return queryFn(array[index]).then(function() { | |
return chain(array, index + 1); | |
}) | |
} | |
}; | |
chain(array, 0); | |
}; | |
sequence(products); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment