How to block until Dojo is allowed to delete?

I know the recommended use case for Dojo. Deferrals should use dojo.when (def) or def.then () and provide a callback for when Deferment is allowed. However, sometimes I come across a scenario where I really need to wait for this Deferral to complete before continuing with the current thread. Here is an example (full example at http://jsfiddle.net/DG3Ax/2/ )

function getSomething() { var def = getSomeDeferred(); def.then(function(result) { dojo.place("<li>def.then() = " + result + "</li>", "output"); }); return def.gimmeTheResultNow(); } dojo.place("<li>getSomething() = " + getSomething() + "</li>", "output"); 

Obviously, Deferred.gimmeTheResultNow() does not exist, but the functionality I'm looking for. I have no control over the code that calls getSomething (), so I cannot get it to handle Deferred; he needs a real result.

I know that xhrGet () has a synchronization parameter that, I think, would do the job if it were an AJAX call, but that is not necessarily the case. Is there any other way to do this?

+6
source share
1 answer

I got a very useful answer from the dojo -interest mailing list, so I thought I would stick with it here:

Unfortunately, you cannot do this in a browser.

JavaScript in the browser is single-threaded. If you are sitting waiting for a deferred decision, then you are using this thread. This is the same thread that you will need somewhere in the queue to service the call to Deferred.resolve () (which in itself will lead to the call to the function that you passed .then ()).

You can call xhr synchronously, because the base implementation of XHR get allows you to call it synchronously. Functionality dojo / Deferred is just a wrapper around the internal elements of XHR.

+4
source

Source: https://habr.com/ru/post/926433/


All Articles