I'm trying to understand how (in what order) promises get executed in protractor, specifically for non-protractor promises (ex. native or q promises) mixed in with protractor promises. I'm debugging & fixing some flaky protractor tests at work, and I am by no stretch of the imagination a JavaScript programmer; so when giving your answer, assume your answer will go over my head and take it down a level or two.
After days of no luck at work with a particular flaky test I decided to write some very simple examples to try to understand how promise chains get executed. From what I've read online I thought chaining like this:
a().then(() => {
x();
b().then(() => {
y();
c().then(() => z());
});
});
should be the same as:
a().then(() => {
x();
b();
}).then(() => {
y();
c();
}).then(() => z());
which should also be the same as this (if I was using ES6 at work, which I'm not):
await a();
await x();
await b();
await y();
await c();
await z();
You can see my full code here along with the output I got: https://github.com/cpjust/TypeScriptTest/tree/dev/specs
I got different results for all of those scenarios and also when using native promises vs protractor promises. In second_spec.ts I also tried adding expect() statements inside promises which should fail and I was expecting the promises that were chained after the expect() not to execute since the expect should throw an assert error, but what I saw is that all promises after it execute, but then the test still fails due to the expect() failure. Very strange...