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() { browser.get(''); browser.sleep(1000) element(by.name('q')).getAttribute("title").then(function(tooltip){ 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) { console.log(response.body); });
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 would be improving this promise article, because i was not feeling like this article needs more explantion
Good Job KartiQ, your blogs are really helpful to me.
thanks Nitish, We are glad these are helping you guys