Patrick Beer - @pabera
Promises are a different way of handling asynchronous functionality and requests without using simple callback functions.
var promise = get('http://amazon.com');promise.done(function(evt) {
console.log(evt);
});.done() can be called as many times you want. It will always return the same once it has been called.
Promise.done(function(content) {}, function(error) {});Chaining promises with .then().
function getContent(url) {
return get(url)
.then(function (content) {
return content;
});
}
getContent('http://amazon.com').done(function (result) {
console.log(result)
}).then() also excepts an error argument
.then( event, error )
function getContent(url) {
return get(url)
.then(function (result) {
return result;
}, function (error) {
if (canRetry(error)) {
return getContent(url);
}
else {
throw error;
}
});
}
getContent('http://www.amazon.com').done(function (content) {
console.log(content);
});.then() (Example 5)var promise = get('http://www.amazon.com')
.then(function (url) {
return get(url);
})
.then(function (result) {
return result;
})
promise.done(function (final) {
console.log(final);
});var a = get('http://www.amazon.com');
var e = get('http://www.ebay.com');
var both = Promise.all([a, e]);
both.done(function (result) {
var a = result[0];
var e = result[1];
console.log({
'amazon': a,
'ebay': e
});
});Useful if you need to run a lot of operations in parallel
Best example: image file preloader
get('http://www.amazon.com').then(function (result) {
console.log('amazon');
console.dir(result);
return get('http://www.ebay.com')
}).done(function (result) {
console.log('ebay');
console.dir(result);
});| fulfilled | The promise related action has successfully been terminated |
| succeeded rejected | goto fail; |
| failed pending | Hasn't fulfilled or rejected yet |
| yet settled | Has fulfilled or rejected |
Standard asynchronous callback paradigm
object.save({ key: value }, {
success: function(object) {
// the object was saved.
},
error: function(object, error) {
// saving the object failed.
}
});We can do it like this with promises.
object.save({ key: value }).then(
function(object) {
// the object was saved.
},
function(error) {
// saving the object failed.
});Callbacks
Parse.User.logIn("user", "pass", {
success: function(user) {
query.find({
success: function(results) {
results[0].save({ key: value }, {
success: function(result) {
// the object was saved.
}
});
}
});
}
});Promises
Parse.User.logIn("user", "pass").then(function(user) {
return query.find();
}).then(function(results) {
return results[0].save({ key: value });
}).then(function(result) {
// the object was saved.
});Error Handling
Parse.User.logIn("user", "pass").then(function(user) {
return query.find();
}).then(function(results) {
return results[0].save({ key: value });
}).then(function(result) {
// the object was saved.
}, function(error) {
// there was some error.
});Create the promise
var promise = new Promise(function(res, rej) {
// Code something great
if (true) {
areWeDone(true);
}
else {
areWeDone(false);
}
});Use it
promise.then(function(result) {
console.log(result); // "areWeDone(true)"
}, function(err) {
console.log(err); // Error: "areWeDone(false)"
});