Asserting values in node.js

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.

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.

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.

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.

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');
}