1

I have a function,

Edit1 - Updated function with real one because the previous one was simplified synchronous function and the code would have worked as correctly pointed by @AlexMA in the comments

'returnSuccessOrFailure': function () {
            return driver.findElement(wd.By.css('div#button')).then(function (button) {
                return button.getAttribute('class').then(function (status) {
                    return status;
                });
            });
        }

In my node.js test, my assertion is failing because the assert is called before returnSuccessOrFailure finishes execution.

var value = returnSuccessOrFailure();
assert.equal(value,'success', 'Looks like something failed');

If I implement a promise in returnSuccessOrFailure and chain my assert then that works. My question is do I have to implement promises all the time for such situations to block the execution? I am new to Javascript and the async nature of it and any insight when to use promises and when not to would be useful.

nilesh
  • 14,131
  • 7
  • 65
  • 79
  • 3
    The code as written would be executed synchronously. Your assertion should not fail (assuming happy==true--I noticed you don't have that assigned). I'm guessing you've got a more complex use-case that does some asynchronous stuff in your test function. – AlexMA Sep 09 '14 at 14:54
  • -1 because the code you posted doesn't actually have the problem described – Robert Levy Sep 09 '14 at 15:06
  • @AlexMA you are right. I tried to simplify the question and missed the whole point :) I will add the actual function which does the asynchronous call. – nilesh Sep 09 '14 at 15:34
  • 1
    @nilesh no worries, simplifying the question is usually a good thing--but you should test your simplified code to make sure the problem still occurs! I often figure out the problem during this step so the question never gets posted :) – AlexMA Sep 09 '14 at 15:35
  • Thanks @RobertLevy for correction and solution as well. I am going to look at all answers tonight. – nilesh Sep 09 '14 at 19:52

4 Answers4

2

you don't have to "implement a promise" in, just return the one you already have:

returnSuccessOrFailure': function () {
            return driver.findElement(wd.By.css('div#button')).then(function (button) {
                ...

but then, yes, you do still need to put your assert in a done handler

returnSuccessOrFailure().done(function(value) {
  assert.equal(value,'success', 'Looks like something failed');
}
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
1

Chaining you asserts will not only make it work but will also make for more readable code. Knowing what happens in what order can be useful when going back to refactor. Not only that but the structure of callbacks/promises also allow for easily written timer tests. Also, since your test needs to have the current state of execution, it is more than likely that writing tests with asserts in callbacks is what you will need anyway.

Jaden Travnik
  • 1,107
  • 13
  • 27
1

My question is do I have to implement promises all the time for such situations to block the execution?

Notice that promises don't block the execution. They defer the execution of code that depends on the result, notice that you're still chaining callbacks on them.

I am new to Javascript and the async nature of it and any insight when to use promises and when not to would be useful.

Promises are useful wherever you have some code that might run asynchronously and needs to pass back an asynchronous result. Otherwise you would need to use callbacks, which are way more ugly than promises.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

This is part of code contracts and representing preconditions (what holds before you execute), postconditions (what holds after you execute), and object invariants (what can not change). JavaScript does not have native support for this, but you can use third party libraries (Cerny.js, ecmaDebug, jsContract, or jscategory)

I think it depends on your coding style, is it EAFP(Easier to ask for forgiveness than permission) or LBYL(Look before you leap). Both are viable! In most compiled languages you would use LBYL. However in Python for example you would use EAFP.

Generally if you know you will fail you want to fail fast. If you like to use assertions to ensure code fails fast it is up to you.

Margus
  • 19,694
  • 14
  • 55
  • 103