Function parameters anti-pattern
Right now the parameters are passed to the handler functions like this:
function(xhr, response)
Isn't this a promise anti-pattern? I have to use intermediate functions to get the response instead of directly manipulating it like:
qwest.get('/someArrayResource')
.then(_.first)
.then(doWhatever);
Having the XHR object as the primary parameter in the function would be useful only in a few situations, where for example you care more about the status code instead of the response, which only happens rarely.
You're right. response should be the first parameter, and xhr should be available with this keyword. I'll plan this for a future major release of qwest. Unless you really need this ASAP, I'll release it in the next months.
This will mess up es6 arrow functions.
this.products = [];
quest.get(....)
.then((xhr, response) => {
this.products = response.products;
});
:/
Hum, indeed... What would you suggest?
Personally I think the API is fine, but if it were to change, just swap the args around.
(response, xhr) effectively makes xhr optional, keeps the response the main thing the user wants/needs, doesn't mess with any scoping in ES5/6
Sometimes anti-patterns are necessary. (not that I consider this an anti-pattern to begin with)
Indeed, It seems the best way to do it. I plan this change for a future major release then. I need to refactor qwest's code a bit, it's kinda messy ^^
Sorry this question is only slightly related but I didn't find the answer in the docs.
If I have:
qwest.get(...)
.then(function (response, xhr) {
return "whatever";
})
.then(function (???) { ... });
The first then returns a promise that is resolved with "whatever", right? So, have I lost xhr at this point? Is there any way to get xhr in subsequent promises?
You're right, at this point xhr is lost. But I can make some method to get it anyway... I should investigate this ^^
It's pretty easy to keep the xhr but you're getting into pyramid territory. Return a new promise chain.
qwest
.get(...)
.then(function (response, xhr) {
return Promise
.resolve(response)
.then(function (???) {
...
});
});