<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.spawn.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.spawn</api:function-name><api:suggest>xdmp.spawn</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>spawn</api:suggest><api:function-link mode="xquery" fullname="xdmp:spawn">/apidoc/8.0/xdmp:spawn.xml</api:function-link><api:function mode="javascript" name="spawn" type="builtin" lib="xdmp" category="Extension" subcategory="XQuery Context" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.spawn"><api:summary>
  Place the specified module on the task queue for evaluation.
</api:summary><api:params><api:param name="path" type="xs:string"><api:param-description>
    The path, relative to the specified root, of the module to be executed.
    The module is considered to be JavaScript if the module path ends with a
    file extension matching the ones configured for
    application/vnd.marklogic-javascript in MarkLogic's Mimetypes
    configuration.  For more details on resolving paths, see "Importing XQuery
    Modules and Resolving Paths" in the <em xmlns="http://www.w3.org/1999/xhtml">Application Developer's Guide</em>.
  </api:param-description><api:param-name>path</api:param-name><api:param-type>String</api:param-type></api:param><api:param name="vars" type="Object" optional="true" class="javascript"><api:param-description>
    The external variable values for this evaluation.
    This can either be a sequence of map:map objects, or a sequence of even
    length, alternating QNames and items.
    <p xmlns="http://www.w3.org/1999/xhtml">Each key in the map(s) is a string representing the name of the parameter
    in Clark notation: "{namespaceURI}localname". The function
    <a href="./xdmp.keyFromQName">xdmp.keyFromQName</a>
    is a convenient way to generate these keys.
    Each entry in the map is the value of the corresponding external 
    variable.</p>
    <p xmlns="http://www.w3.org/1999/xhtml">Alternatively, the alternating sequence should contain QName and item
    pairs that specify a variable name and value for an external variable.</p>
  </api:param-description><api:param-name>vars</api:param-name><api:param-type>Object</api:param-type></api:param><api:param name="options" type="(element()|map:map)?" optional="true"><api:param-description>
  
  <span class="javascript" xmlns="http://www.w3.org/1999/xhtml">The options object. The default value is null. 
  See the
  <a href="./xdmp.eval"><code>xdmp.eval</code></a> section for a list of options.</span>
  </api:param-description><api:param-name>options</api:param-name><api:param-type>Object?</api:param-type></api:param></api:params><api:return>ValueIterator</api:return><api:usage>
    <p xmlns="http://www.w3.org/1999/xhtml">This function places the specified XQuery module
    in the task queue to be processed. The module will be evaluated when the
    task server has the available resources to process it. The tasks are
    processed in the order in which they are added to the queue. </p>
    <p xmlns="http://www.w3.org/1999/xhtml">Once 
    <code class="javascript">xdmp.spawn</code> is called, it cannot be 
    rolled back,
    even if the transaction from which it is called does not complete.
    Therefore, use care or preferably avoid calling this function
    from a module that is performing an update transaction.  Once a module is
    spawned, its evaluation is completely asynchronous of the statement in
    which 
    <code class="javascript">xdmp.spawn</code> was called. Consequently,
    if you call this function from a module that is updating a
    document, and if the update ends up retrying (for example, if a deadlock
    is detected), then the entire module is re-evaluated and the
    
    <code class="javascript">xdmp.spawn</code> call is therefore called again.
    This will only happen in update statements, not in query statements. 
    For details
    on how transactions work in MarkLogic Server, see "Understanding
    Transactions in MarkLogic Server" in the <em>Developer's Guide</em>.</p>
</api:usage><api:privilege>
<p xmlns="http://www.w3.org/1999/xhtml"><code>http://marklogic.com/xdmp/privileges/xdmp-spawn</code></p>
</api:privilege><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
  xdmp.spawn("module.xqy", null,
        {
          "modules" : xdmp.modulesDatabase(),
          "root" : "http://example.com/application/"
        })

  =&gt; Puts the module from the modules database with the
     URI http://example.com/application/module.xqy
     in the task server queue.</pre>
</api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml"><a id="spawnresultex"></a>
// This example uses the "result" option to use the results of a
// spawned task in the query

var x = xdmp.spawn("/oneplusone.sjs", null, {result: true})
// because xdmp.spawn returns a ValueIterator, 
// use next().value to get the returned value
x.next().value + 2

// If  "/oneplusone.sjs" has following code: 
//    1 + 1 
// then this returns 4 
</pre></api:example></api:function></api:function-page>