by codedokode · 8y ago · 0 comments
He doesn't like that the callback is called immediately - but Promises just represent a result that will be available later and do not guarantee (and should not) when the function will be called. If you want to delay some function call, do it explicitly or use a delay promise.
In my opinion, the main problem with promises is broken error handling. They don't play well with exceptions. For example:
var p = Promise(function (res, rej) {
throw new RuntimeError("System is broken");
});
This code will just ignore the error. While it is expected that the runtime error would float up and terminate the program - that is what runtime errors are made for.This also makes writing tests more difficult because tests often use exceptions to indicate failure.
I have some ideas how to fix it (neither is perfect), but the comment will become too long.
e.g. consider this python code:
def start_task():
Thread(do_task).start()
What happens if do_task throws an exception? The exception can't propagate up from start_task because start_task may have returned when the exception is thrown.At some point you have to join your threads, and you have to await/then your promises, otherwise there's no well defined place in your program for the exception to go.
Linters can help with this. tslint is one example, I believe it can give a warning for unhandled promises.
`p` will reject, any function that awaits p will reject. The error is that you've fired off an asynchronous task but you don't have any code that cares about the result of that asynchronous task (and the stack of any code that
Two Erlang processes (~actors) can be linked together. If one then crashes (uncaught exception), the other one is automatically killed - unless it takes special steps to "trap exits", in which case it is sent a message describing the failure in its linked peer.
I don't see the problem with that. Unhandled exceptions can occur at any place of your program.
I also don't see why the unhandled exception from the background thread cannot terminate main thread. Why not? That is how unhandled exceptions are supposed to work. Terminating the program is the optimal default behaviour for any error in my opinion. This way you won't miss them.
This is how unhandled promise rejections will be handled in node soon. In the browser, you can declare an event handler for unhandled promise rejections.
- return an error
- panic
What is the recommended, best, pretty, beautiful way? Return an error. Nobody uses panic.Javascript Promises are equivalent to that.
If you want to stop the main program, call process.exit(). That would be the equivalent of panic.
def start_task():
thread = Thread(do_task)
thread.set_error_handler(SpecificErrorType, handler)
thread.start() function startTask() {
const promise = doTask();
promise.catch((e) => {
if (e instanceof SpecificErrorType) {
handler(e);
} else {
throw e;
}
});
}This will supposedly happen in a future version of node. [1]
[1]: https://nodejs.org/dist/latest-v8.x/docs/api/deprecations.ht...
No, this behavior is consistent with how asynchronous programming works in JS.
myDiv.addEventListener("click",function(event){
throw "error";
});
Nobody would expect that code to terminate a browser tab on error.> This also makes writing tests more difficult because tests often use exceptions to indicate failure.
Then use async functions, problem solved.
That is because browser environment catches all exceptions and displays them in console. So effectively they become handled exceptions.
No, it has nothing to do with the developer console. It has everything to do with the fact that async exceptions do not bubble up in the main execution thread. Promises work the exact same way since they existed as libraries long before they were included in the language. It's about consistency, nothing more, nothing less.
Error handling is annoying, and this is (eventually going to be) fixed in Node.js. In the browser, you can add some listeners: http://2ality.com/2016/04/unhandled-rejections.html
The receiver of the promise is the one who should handle rejection. Not the one creating the promise.
If the receiver doesn't handle it you can register global error handler and log all unhandled errors.