2

In the following code why is promise resolved displayed before promise being resolved?

    var p = protractor.promise.defer();

    element(by.css("input.m-call-to-action")).click().then(function() {
        console.log('promise being resolved');
        p.fulfill(true);
    });

    browser.wait(function() {
        console.log('p', p);
        return p;
    }, 5000);

    console.log('promise resolved', p);
z1naOK9nu8iY5A
  • 903
  • 7
  • 22
  • possible duplicate of [How to wait for the backend in Protractor?](http://stackoverflow.com/questions/28808137/how-to-wait-for-the-backend-in-protractor) – P.T. Mar 03 '15 at 18:11

1 Answers1

3

This is basically what Control Flow documentation page is describing:

WebDriverJS (and thus, Protractor) APIs are entirely asynchronous. All functions return promises.

Protractor maintains a control flow queue where your click() promise is at the beginning and browser.wait() is at the end. When promise resolved is logged none of the promises in the queue are resolved.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Why putting expect(true).toBeTruthy(); before the last console.log does not force both wait and the then to be executed before the last log? – z1naOK9nu8iY5A Mar 02 '15 at 15:44
  • @z1naOK9nu8iY5A is the behavior consistent? `expect`, btw, is "patched" to resolve a promise also. – alecxe Mar 02 '15 at 18:25
  • Is there a way to resolve all promises and their callbacks before calling ``console.log('promise resolved', p);`` ? – z1naOK9nu8iY5A Mar 03 '15 at 09:18
  • @z1naOK9nu8iY5A yup, using `q.all()` or `protractor.promise.all()`, see examples at https://github.com/angular/protractor/issues/639, http://stackoverflow.com/a/21736757/771848. – alecxe Mar 03 '15 at 12:54