It should be noted that there are no synchronous calls available in the JS Object Model, and this is on purpose. The proposal to use promises is just a different async pattern, but that's okay…
It is possible to artificially 'force' the scripts to work in a synchronous manner using various techniques, but there's really no reason to.
Async patterns (direct callback from executeQueryAsync
or the various implementations of the promise pattern) are setup to perform the work you need to accomplish and allow your page to perform well without blocking the UI process -- that is the whole point. A traditional execution environment (an application running on the server/client machine) makes it possible to run many different tasks simultaneously, but working with JavaScript in the browser you only really get a single execution process to work with (web workers being the notable exception).
Working with executeQueryAsync
is actually quite straight forward, it just follows a slightly different execution path when you’re reading down your code than using a synchronous model.
In general the pattern works like this:
- Load one or many client objects (
SP.ClientContext.load(object)
) to
instruct SharePoint you want to retrieve one or many pieces of
information.
- Instruct the framework to execute
executeQueryAsync
and provide
two callbacks as parameters; the first for the case where the call
succeeds, and the second in case the calls encounters an error
- Inside of your initial success callback, continue the optimal
execution path – perhaps loading more client objects and calling
executeQueryAsync
again and providing success and failure handlers
once again (these could be the same functions over and over, or
separate functions, or anonymous functions defined inline)
The execution flow can get messy if you’re not careful, but does not need to be significantly more complicated than a standard synchronous format.
JS execution with the JSOM in SharePoint works quite well with the async patterns provided – I encourage people to give them a chance even though they’re not what you might be used to!