...">

Dojo return value from inside requires

How do I return a value from a dojo request block? Say I have a setting like this:

<div class = "people"> <span class = "name"> Joe Bloggs </span> the Baker <br /> <span class = "name"> John Smith </span> the Chef <br /> </div> function getNames () { require(["dojo/query", "dojo/domReady!"], function(query) { var names = []; query (".name").forEach (function (node) { names.push (node.innerHTML); }); return names; }); } function doSomethingWithNames () { var names = getNames (); // names is always undefined } 

I know why this happens - the require block is executed asynchronously, so the variable names in doSomethingWithNames are assigned before returning getNames. But how do I get around this?

+4
source share
1 answer

Add custom callback:

 function getNames (callback) { require(["dojo/query", "dojo/domReady!"], function(query) { var names = []; query (".name").forEach (function (node) { names.push (node.innerHTML); }); callback(names); }); } function doSomethingWithNames () { getNames (function(names) { //do your stuff with the return here //names will now be populated }); } 
+4
source

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


All Articles