I need to execute some synchronous code after an asynchrnous Promise is resolved. Currently my code looks like this:
console.log("Starting");
promiseFunc().
then(function() {
console.log("PromiseFunc done. Doing Synchronous work...");
synchronousFunc1();
synchronousFunc2();
console.log("Synchronous work done");
}).
catch(function(err) {
console.log("Error:" + err);
}).
then(function() {
console.log("Done")
});
Everything is working fine: The first then
is executed after promiseFunc
is resolved and the final then
is executed last.
I think the spec expects me to return a new Promise or a value from the then
, but there is nothing useful I can return.
Here are my questions:
- Does this 'pattern' for executing synchronous code after a promise is resolved make sense? Is it okay to return undefined from the
then
block? - What happens if someone decides to implement
synchronousFunc1
with a promise? This will break the order of execution. I think such a breaking change shouldn't be made. Instead anotherasynchronousFunc1
should be implemented. Am I right? - What about my
catch/then
implementation? The finalthen
should be executed always. I think this will not work, if an exception is thrown in thecatch
handler. Are there alternatives? I know there is afinally
handler in bluebird, but I want to use standard Promise features only.