@CAYdenberg
CAYdenberg
ydenberg@gmail.com
caseyy.org
Eleven: Promise?
Mike: It's something you can't break. Ever.
We are committed to ensuring that 2015 will be the last federal election conducted under the first-past-the-post voting system.
A promise is a container for an asynchronously delivered value
$data = file_get_contents('http://a/resource/somewhere');
http.get('http://a/resource/somewhere', function(res) {
// deal with response
});
http.get('http://a/resource/somewhere', function(res) {
// deal with response
});
httpPromise.get('http://a/resource/somewhere').then(function(res) {
// deal with response
});
Since Node.js 0.12
Polyfill: https://github.com/stefanpenner/es6-promise
... Babel
function wait(time) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve('You kept your Promise!');
}, time);
});
}
wait(1000).then(function(msg) {
console.log(msg)
});
function fetchUrl(url) {
return new Promise(function(resolve, reject) {
http.get(url, function(response) {
if (response.status === 200) {
return resolve(response.body);
else {
return reject(Error('Not Found'));
}
});
});
}
const url = 'https://api.forecast.io/forecast/APIKEY/50.1149639,-122.9486474';
fetchUrl(url).then(function(data) {
console.log(data);
}, function(err) {
console.log(err);
});
... or ...
fetchUrl(url).then(function(data) {
console.log(data);
}).catch(function(err) {
console.log(err);
});
Imagine you're getting some data from a database, and then processing it asynchonously:
getDataFromDb(params, function(data) {
processData(data, function(processedData) {
// deal with data
});
});
If we encapsulate this logic in a function, what do we return?
function queryDb(params) {
return new Promise(resolve, reject) {
getDataFromDb(params, function(data) {
processData(data, function(processedData) {
resolve(processedData);
});
});
});
}
queryDb.then(...);
.then
always returns another promise. Very useful.
myPromise.then(function(dataOne) {
// process dataOne and create another promise
return myOtherPromise(dataOne);
}).then(function(dataTwo) {
// do something with dataTwo
});
Chaining advantages:
.catch
at the end will handle error anywhere in the chain.
var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise(function(resolve, reject) {
setTimeout(function() {
return "Resolved"
}, 1000);
});
Promise.all([p1, p2, p3]).then(function(values) {
console.log(values); // [3, 1337, "Resolved"]
});
What the actual flying fuck? Promises swallows errors by default! Who’s idea was that?
new Promise(function(resolve, reject) {
resolve('promised value');
}).then(function(value) {
throw Error('error');
}).catch(function(error) {
console.error(error.stack);
});
http://jamesknelson.com/are-es6-promises-swallowing-your-errors/