How to use .finally() without .then() in AngularJS?












1















I want a block of code to run after every resolution/rejection of a promise (using $q in AngularJS 1.6.x).



I know I can do:



myPromise()
.then((response) => {
// Do my important stuff here
})
.catch((response) => {
// Copy code from above
})


As well as this (slightly better):



myPromise()
.catch(() => {})
.then((response) => {
// Do my important stuff here, and only write it once
})


But I just want to write something like:



myPromise()
.finally((response) => {
// Do my important stuff here, only written once
})


But it seems .finally() will not run without a then() or catch() block handling the resolution/rejection first.



Any way to do this?










share|improve this question























  • this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

    – Claies
    Nov 28 '18 at 2:03











  • I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

    – Tony Brasunas
    Nov 28 '18 at 5:10






  • 1





    The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

    – georgeawg
    Nov 28 '18 at 5:10













  • @Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

    – Tony Brasunas
    Nov 28 '18 at 5:52











  • Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

    – Tony Brasunas
    Nov 28 '18 at 5:54
















1















I want a block of code to run after every resolution/rejection of a promise (using $q in AngularJS 1.6.x).



I know I can do:



myPromise()
.then((response) => {
// Do my important stuff here
})
.catch((response) => {
// Copy code from above
})


As well as this (slightly better):



myPromise()
.catch(() => {})
.then((response) => {
// Do my important stuff here, and only write it once
})


But I just want to write something like:



myPromise()
.finally((response) => {
// Do my important stuff here, only written once
})


But it seems .finally() will not run without a then() or catch() block handling the resolution/rejection first.



Any way to do this?










share|improve this question























  • this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

    – Claies
    Nov 28 '18 at 2:03











  • I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

    – Tony Brasunas
    Nov 28 '18 at 5:10






  • 1





    The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

    – georgeawg
    Nov 28 '18 at 5:10













  • @Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

    – Tony Brasunas
    Nov 28 '18 at 5:52











  • Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

    – Tony Brasunas
    Nov 28 '18 at 5:54














1












1








1








I want a block of code to run after every resolution/rejection of a promise (using $q in AngularJS 1.6.x).



I know I can do:



myPromise()
.then((response) => {
// Do my important stuff here
})
.catch((response) => {
// Copy code from above
})


As well as this (slightly better):



myPromise()
.catch(() => {})
.then((response) => {
// Do my important stuff here, and only write it once
})


But I just want to write something like:



myPromise()
.finally((response) => {
// Do my important stuff here, only written once
})


But it seems .finally() will not run without a then() or catch() block handling the resolution/rejection first.



Any way to do this?










share|improve this question














I want a block of code to run after every resolution/rejection of a promise (using $q in AngularJS 1.6.x).



I know I can do:



myPromise()
.then((response) => {
// Do my important stuff here
})
.catch((response) => {
// Copy code from above
})


As well as this (slightly better):



myPromise()
.catch(() => {})
.then((response) => {
// Do my important stuff here, and only write it once
})


But I just want to write something like:



myPromise()
.finally((response) => {
// Do my important stuff here, only written once
})


But it seems .finally() will not run without a then() or catch() block handling the resolution/rejection first.



Any way to do this?







angularjs q






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 1:51









Tony BrasunasTony Brasunas

1,25411625




1,25411625













  • this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

    – Claies
    Nov 28 '18 at 2:03











  • I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

    – Tony Brasunas
    Nov 28 '18 at 5:10






  • 1





    The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

    – georgeawg
    Nov 28 '18 at 5:10













  • @Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

    – Tony Brasunas
    Nov 28 '18 at 5:52











  • Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

    – Tony Brasunas
    Nov 28 '18 at 5:54



















  • this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

    – Claies
    Nov 28 '18 at 2:03











  • I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

    – Tony Brasunas
    Nov 28 '18 at 5:10






  • 1





    The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

    – georgeawg
    Nov 28 '18 at 5:10













  • @Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

    – Tony Brasunas
    Nov 28 '18 at 5:52











  • Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

    – Tony Brasunas
    Nov 28 '18 at 5:54

















this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

– Claies
Nov 28 '18 at 2:03





this doesn't exactly make sense. Are you essentially saying you don't care if the response is a success or failure and just want to do the same thing in both cases? what happens when the failure creates a situation that causes your code to be not runnable? It seems like you are misunderstanding how .then and .catch work here.

– Claies
Nov 28 '18 at 2:03













I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

– Tony Brasunas
Nov 28 '18 at 5:10





I suppose it's an unusual case. I'm running a polling service that will poll a given API endpoint every x milliseconds while the back-end server is processing data until it either receives a "succeeding" report, a "failing" report, or doesn't receive any report at all after y attempts. Whether the succeeding or failing report returns, the response has data that I need to process in essentially the same way. So yes in this case I need to process the data nearly the same way regardless of whether the polling service returns with resolve or reject.

– Tony Brasunas
Nov 28 '18 at 5:10




1




1





The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

– georgeawg
Nov 28 '18 at 5:10







The .finally() method does not expose a response and it does not create a new promise from what is returned. It does in fact work without a preceding .then or .catch method. Please explain your allegation that is "does not work".

– georgeawg
Nov 28 '18 at 5:10















@Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

– Tony Brasunas
Nov 28 '18 at 5:52





@Claies I think you're on to something come to think about it. I'm going to slightly rewrite the polling service definition function (which determines success vs failure) rather than treat bad data as a failure. Even unusable data is a successful response from the standpoint of the flow of the code. That way I can use .then() and .catch() in the standard way.

– Tony Brasunas
Nov 28 '18 at 5:52













Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

– Tony Brasunas
Nov 28 '18 at 5:54





Haven't decided whether to remove this question or whether it would be useful to anyone to leave it here. Point being, no you can't use .finally() without a .catch() or a .then()`. But that's just fine, at least in this case.

– Tony Brasunas
Nov 28 '18 at 5:54












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53510912%2fhow-to-use-finally-without-then-in-angularjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53510912%2fhow-to-use-finally-without-then-in-angularjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista