Thomas Boyt
Front-end engineer
$.get('/api/search/kortina', function(payload) {
var user = payload.data[0];
alert('user id is ' + user.id);
});
$.get('/api/search/kortina', function(payload) {
var user = payload.data[0];
alert('user id is ' + user.id);
}, function(err) {
alert('Hey, everything broke!');
});
function searchUser(username, cb, errCb) {
$.get('/api/search/' + username, function(payload) {
var user = payload.data[0];
cb(user);
}, err);
}
searchUser('kortina', function(user) {
alert('user id is ' + user.id);
}, function(err) {
alert('Hey, everything broke!');
});
function searchUser(username, cb, err) { $.get('/api/search/' + username, function(payload) { var user = payload.data[0]; cb(user); }, err); }
function payUserId(id, amount, cb, err) { $.post('/api/v5/payments/', { user_id: 1, amount: amount }, function(payload) { cb(payload.balance); }, err); } searchUser('kortina', function(user) { payUserId(user.id, 1.00, function(balance) { alert('Made payment; your balance is now ' + balance); }, function() { alert('error making payment ' + err.reason); }); }, function(err) { alert('error finding user ' + err.reason); });
function searchUser(username) { return $.get('/api/search/' + username) .then(function(payload) { var user = payload.data[0]; return user; });
}
function payUserId(id, amount) {
return $.post('/api/v5/payments/', { user_id: 1, amount: amount })
.then(function(payload) {
return payload.balance;
});
}
searchUser('kortina')
.then(function(user) {
return payUserId(user.id, 1);
}, function(err) {
alert('error finding user ' + err.reason);
})
.then(function(balance) {
alert('Made payment; your balance is now ' + balance);
}, function(err) {
alert('error making payment ' + err.reason);
});
this.user.save()
.then(this.user.setAccessTokenCookie.bind(this.user))
.then(this.user.createDjangoSession.bind(this.user))
.then(this.user.sendCode.bind(this.user))
.then(this.handleSuccess, this.handleFailure);
function getNewestDiaryEntry() {
return $.get('/api/secret')
.then(function(payload) {
// $.get returns a promise...
return $.get('/api/my_secret_diary/' + payload.secret);
});
.then(function(payload) {
// ...but here we just return a value...
return payload.entries[0];
})
}
// ...but we can still chain off of it!
getNewestDiaryEntry()
.then(function(entry) {
alert('Newest diary entry title: ' + entry.title);
});
q($.get('/api/v5/me')).then(...);
q($.get('/api/v5/me'))
.then(function(payload) {
console.log('got user', pyload); // whoops, typo'd payload!
}, function(err) {
alert('an error happened');
});
q($.get('/api/v5/me'))
.then(function(payload) {
console.log('got user', pyload); // whoops, typo'd payload!
}, function(err) {
alert('an error happened in $.get');
})
.then(null, function(err) {
console.log('an error happened in the success callback');
throw err; // *rethrow* error so it's visible in console & stops execution
});
q($.get('/api/v5/me'))
.then(function(payload) {
console.log('got user', pyload); // whoops, typo'd payload!
}, function(err) {
alert('an error happened in $.get');
})
.catch(function(err) {
console.log('an error happened in the success callback');
throw err; // *rethrow* error so it's visible in console & stops execution
});
.done
.done
at the end of a promise chain.then
, but you cannot chain off of it
.done
will not catch errors in the success callback, meaning they will be rethrown as you expectq($.get('/api/v5/me'))
.done(function(payload) {
// this will actually error!
console.log('got user', pyload); // whoops, typo'd payload!
}, function(err) {
alert('an error happened in $.get');
});
.done
is an addition to Promises/A+ specific to the Q library, and is not "portable." It may not exist in other promises libraries and does not exist in ES6 promises..then
multiple times off of the same promisevar xhr = q.delay(50);
xhr.then(function() {
console.log('I get called first!');
}).then(function() {
console.log('I get called third!');
});
xhr.then(function() {
console.log('I get called second!');
});
q.all([
get('/api/v5/me'),
get('/api/v5/news')
]).then(function(results) {
var user = results[0];
var news = results[1];
// ...
});
In ES6 as Promise.all()!
q.all([
get('/api/v5/me'),
get('/api/v5/news')
]).spread(function(user, news) {
// ...
});
By Thomas Boyt