TSLint - Promises must be handled appropriately with `finally`
I'm getting this error from TSLint and I'm trying to understand why it is complaining about.
I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.
I've simplified it to this function and just use Q()
to simulate an invocation that returns a promise.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
}).finally(() => {
log.info("at finally")
});
}
When I run tslint
on my project now I'm getting the following error:
ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately
If I remove the finally
call tslint passes without errors.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
});
}
When I create the same function on a seed typescript project this behavior does not reproduce...
typescript q tslint
add a comment |
I'm getting this error from TSLint and I'm trying to understand why it is complaining about.
I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.
I've simplified it to this function and just use Q()
to simulate an invocation that returns a promise.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
}).finally(() => {
log.info("at finally")
});
}
When I run tslint
on my project now I'm getting the following error:
ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately
If I remove the finally
call tslint passes without errors.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
});
}
When I create the same function on a seed typescript project this behavior does not reproduce...
typescript q tslint
add a comment |
I'm getting this error from TSLint and I'm trying to understand why it is complaining about.
I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.
I've simplified it to this function and just use Q()
to simulate an invocation that returns a promise.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
}).finally(() => {
log.info("at finally")
});
}
When I run tslint
on my project now I'm getting the following error:
ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately
If I remove the finally
call tslint passes without errors.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
});
}
When I create the same function on a seed typescript project this behavior does not reproduce...
typescript q tslint
I'm getting this error from TSLint and I'm trying to understand why it is complaining about.
I have a function which invokes another method which returns a promise but the first function does not return the promise because it just waits for it to finish and update an internal state.
I've simplified it to this function and just use Q()
to simulate an invocation that returns a promise.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
}).finally(() => {
log.info("at finally")
});
}
When I run tslint
on my project now I'm getting the following error:
ERROR: C:/dev/local_cache_service.ts[31, 5]: Promises must be handled appropriately
If I remove the finally
call tslint passes without errors.
export function DoSomethingAsync(): void {
Q().then(r => {
console.log('test');
}).catch(err => {
log.error("wow");
});
}
When I create the same function on a seed typescript project this behavior does not reproduce...
typescript q tslint
typescript q tslint
edited Nov 26 '18 at 8:01
Ido Ran
asked Nov 26 '18 at 6:48
Ido RanIdo Ran
4,346851103
4,346851103
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is a complaint from the no-floating-promises rule. As per its description:
Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.
It’s typically better to return Promises from functions that start them, then handle them in calling code.
Use
no-unused-expression
in addition to this rule to reveal even more floating promises.
Specifically in your case, it's because you're running code in a .finally
block after the .catch
block. That's considered dangerous by the rule because if the code within the .finally
throws an error, it'll be unhandled by the calling code.
The best thing for you to do would be to return
the promise, so the function's return type is a Promise/Q instead of void
.
Tip: you can run tslint -t stylish
or tslint -t verbose
to see rule names along with their complaints!
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a.catch
after the.finally
, which will handle the case of the.finally
code throwing an error You could also consider having something that receives these floating promises and.catch
es them.safePromiseCatcher(Q().then(...));
.
– Josh
Nov 27 '18 at 19:51
add a comment |
The solution was to add a call to .done()
at the end of the promise call chain.
From what I understood done
converts any unhandled exceptions to a regular unhandled exception.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475974%2ftslint-promises-must-be-handled-appropriately-with-finally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a complaint from the no-floating-promises rule. As per its description:
Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.
It’s typically better to return Promises from functions that start them, then handle them in calling code.
Use
no-unused-expression
in addition to this rule to reveal even more floating promises.
Specifically in your case, it's because you're running code in a .finally
block after the .catch
block. That's considered dangerous by the rule because if the code within the .finally
throws an error, it'll be unhandled by the calling code.
The best thing for you to do would be to return
the promise, so the function's return type is a Promise/Q instead of void
.
Tip: you can run tslint -t stylish
or tslint -t verbose
to see rule names along with their complaints!
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a.catch
after the.finally
, which will handle the case of the.finally
code throwing an error You could also consider having something that receives these floating promises and.catch
es them.safePromiseCatcher(Q().then(...));
.
– Josh
Nov 27 '18 at 19:51
add a comment |
This is a complaint from the no-floating-promises rule. As per its description:
Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.
It’s typically better to return Promises from functions that start them, then handle them in calling code.
Use
no-unused-expression
in addition to this rule to reveal even more floating promises.
Specifically in your case, it's because you're running code in a .finally
block after the .catch
block. That's considered dangerous by the rule because if the code within the .finally
throws an error, it'll be unhandled by the calling code.
The best thing for you to do would be to return
the promise, so the function's return type is a Promise/Q instead of void
.
Tip: you can run tslint -t stylish
or tslint -t verbose
to see rule names along with their complaints!
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a.catch
after the.finally
, which will handle the case of the.finally
code throwing an error You could also consider having something that receives these floating promises and.catch
es them.safePromiseCatcher(Q().then(...));
.
– Josh
Nov 27 '18 at 19:51
add a comment |
This is a complaint from the no-floating-promises rule. As per its description:
Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.
It’s typically better to return Promises from functions that start them, then handle them in calling code.
Use
no-unused-expression
in addition to this rule to reveal even more floating promises.
Specifically in your case, it's because you're running code in a .finally
block after the .catch
block. That's considered dangerous by the rule because if the code within the .finally
throws an error, it'll be unhandled by the calling code.
The best thing for you to do would be to return
the promise, so the function's return type is a Promise/Q instead of void
.
Tip: you can run tslint -t stylish
or tslint -t verbose
to see rule names along with their complaints!
This is a complaint from the no-floating-promises rule. As per its description:
Creating a Promise and not storing or returning it may let other code run independently of its result. This can cause unexpected and/or non-deterministic behavior depending on external timing factors.
It’s typically better to return Promises from functions that start them, then handle them in calling code.
Use
no-unused-expression
in addition to this rule to reveal even more floating promises.
Specifically in your case, it's because you're running code in a .finally
block after the .catch
block. That's considered dangerous by the rule because if the code within the .finally
throws an error, it'll be unhandled by the calling code.
The best thing for you to do would be to return
the promise, so the function's return type is a Promise/Q instead of void
.
Tip: you can run tslint -t stylish
or tslint -t verbose
to see rule names along with their complaints!
answered Nov 27 '18 at 4:23
JoshJosh
821512
821512
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a.catch
after the.finally
, which will handle the case of the.finally
code throwing an error You could also consider having something that receives these floating promises and.catch
es them.safePromiseCatcher(Q().then(...));
.
– Josh
Nov 27 '18 at 19:51
add a comment |
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a.catch
after the.finally
, which will handle the case of the.finally
code throwing an error You could also consider having something that receives these floating promises and.catch
es them.safePromiseCatcher(Q().then(...));
.
– Josh
Nov 27 '18 at 19:51
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
Thank you. Return the promise will not solve the problem, it will just move it to the called of this function, which in this case is a private function and by design I don't want to return this promise as it's an internal operation that the caller should not wait on.
– Ido Ran
Nov 27 '18 at 8:30
In that case, two quick options: * Put a
// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a .catch
after the .finally
, which will handle the case of the .finally
code throwing an error You could also consider having something that receives these floating promises and .catch
es them. safePromiseCatcher(Q().then(...));
.– Josh
Nov 27 '18 at 19:51
In that case, two quick options: * Put a
// tslint:disable-next-line:no-floating-promises
above the line being complained about * Put a .catch
after the .finally
, which will handle the case of the .finally
code throwing an error You could also consider having something that receives these floating promises and .catch
es them. safePromiseCatcher(Q().then(...));
.– Josh
Nov 27 '18 at 19:51
add a comment |
The solution was to add a call to .done()
at the end of the promise call chain.
From what I understood done
converts any unhandled exceptions to a regular unhandled exception.
add a comment |
The solution was to add a call to .done()
at the end of the promise call chain.
From what I understood done
converts any unhandled exceptions to a regular unhandled exception.
add a comment |
The solution was to add a call to .done()
at the end of the promise call chain.
From what I understood done
converts any unhandled exceptions to a regular unhandled exception.
The solution was to add a call to .done()
at the end of the promise call chain.
From what I understood done
converts any unhandled exceptions to a regular unhandled exception.
answered Nov 27 '18 at 14:35
Ido RanIdo Ran
4,346851103
4,346851103
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475974%2ftslint-promises-must-be-handled-appropriately-with-finally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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