| I have a feeling the author doesn't understand Promises well. In my opinion, they are in fact designed poorly, but I don't see any problems with points the author describes. 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:
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