What do you prefer when writing promises?
caches.open('cache').then(function (cache) {
cache.put(event.request, clonedResponse);
});
caches.open('cache')
.then(function (cache) {
cache.put(event.request, clonedResponse);
});
Some things that could alter the answer could be the catch, i found the second one clearer when using catch, but i also find the first one more clear when catch is ommited. Also if the promise is long like: someObjectThatContains.someMethod(SomeVariable), chaining then in the same line can slip past the then method.
Anyway, Do you have preference?
In general, every promise chain should have a .catch on it to ensure errors are handled.
That said, I think either approach is fine, and it likely depends on the specific context.
So both of this are ok? That doesn't seem consistent :(
caches.open('cache').then(function (cache) {
cache.put(event.request, clonedResponse);
}).catch(function(e) {
//
};
caches.open('cache')
.then(function (cache) {
cache.put(event.request, clonedResponse);
})
.catch(function(e) {
//
});
(Btw ignore the other rules, i know i'm not using arrows, etc)
The goal is readability and consistency, and occasionally one takes a small hit in favor of the other.
I suppose given those two I'd lean towards the former, but I don't think we have an official stance on it.
Most helpful comment
The goal is readability and consistency, and occasionally one takes a small hit in favor of the other.
I suppose given those two I'd lean towards the former, but I don't think we have an official stance on it.