Join us on :

Protractor Promise

Subscribe to my youtube channel :

Promises in Protractor

A Promises object represents a value that may not be available yet, but will be resolved at some point in the future

A promise is an object returned by a module, library, function, that will be resolved or rejected some time in the future.

That's the point: an object that will be resolved in the future. You can then use additional functions that will be executed once that promise is resolved (so, not now with your current code).

You can think of promises as helpful abstraction on top of some operation that cannot be performed synchronously, or would be more performant to be executed asynchronously.

Promises are, as their name implies, a promise that you will have a return value, though it might not be immediate.

It gives you a .then method, to which you can pass your callbacks, or functions that will be executed once the promise is resolved/rejected.

										import { browser, element, by} from 'protractor'
										import { protractor } from 'protractor/built/ptor';
										describe('Protractor Typescript Demo', function() {
											browser.ignoreSynchronization = true;
											it('click operation', function() {
													expect(tooltip).toBe("Search", "tooltip is not 'Search'")

You can pass many callbacks (other functions) to it's .then() method. Each of those will be executed when Google responds to that query.

promise.then(function(response) {

Repeat that (or pass another functions) as many times as you need. You can even pass the `promise` object to some other function or module which will also be able to attach listeners.

Once the promise is resolved (ie. Google responds to that query), all those callbacks get executed, and they get the resolved value as a parameter.

There's also a chance that the promise will be rejected, usually when there's an error or the input to your module is wrong. You can pass a callback for that case too, as a second parameter to the .then() method.

promise.then(null, function(error) {
        console.log('There has been an error in the request: ', err);

Or pass both success and error handlers together:

promise.then(someFunction, someOtherFunction);		

There's a chance that your promise is never going to be resolved or rejected. In that case, your handlers will never be called. You might want to cover this case with some timeout or a similar function. Some modules, like request, have a built-in timeouts.

promise.then(someFunction, someOtherFunction);		

The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:

  • pending - The initial state of a promise.
  • fulfilled - The state of a promise representing a successful operation.
  • rejected - The state of a promise representing a failed operation.
  • Using promises is more of a design pattern choice.
  • For simple projects it is not needed (unless the project intends to be used in other large-codebase projects.)
  • Mixing promise pattern and callbacks within same project could end up being confusing for other developers to read or contribute to the code

Protractor Interview Questions

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • onotolei
    It would be  nice to mention about async/await syntax as well.
    • karthiQ [ admin]
      I would be improving this promise article, because i was not feeling like this article needs more explantion
  • Nitish
    Good Job KartiQ, your blogs are really helpful to me.
    • karthiQ [ admin]
      thanks Nitish,
      We are glad these are helping you guys