Not this this is not synchronous, you need to have the returned promise not resolve until the command is completed. Have a look at other examples where promises are used.

It may also clean up your code above to use promises instead of such heavy nesting.