--- title: WebAssembly.instantiateStreaming() slug: Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming --- <div>{{JSRef}}</div> <p><strong><code>WebAssembly.instantiateStreaming()</code></strong> 方法直接从流式底层源编译和实例化WebAssembly模块。这是加载wasm代码一种非常有效的优化方式。</p> <h2 id="Syntax">Syntax</h2> <pre class="syntaxbox">Promise<ResultObject> WebAssembly.instantiateStreaming(<em>source</em>, <em>importObject</em>);</pre> <h3 id="Parameters">Parameters</h3> <dl> <dt><em>source</em></dt> <dd>一个{{domxref("Response")}}对象 或 一个可以履行(fulfill)一个(Response)的 {{jsxref("Promise")}},表示你想要传输、编译和实例化的 .wasm 模块基础源。</dd> <dt><em>importObject</em> {{optional_inline}}</dt> <dd>包含一些想要导入到新创建<code>Instance</code>中值的对象,例如方法 或 {{jsxref("WebAssembly.Memory")}} 对象。每个已编译模块的声明导入必须有一个匹配属性,否则抛出 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/LinkError">WebAssembly.LinkError</a> 异常。</dd> </dl> <h3 id="Return_value">Return value</h3> <p>一个 <code>Promise</code> ,通过resolve返回一个包含两个属性的 <code>ResultObject</code> :</p> <ul> <li><code>module</code>: {{jsxref("WebAssembly.Module")}} 对象表示编译完成的WebAssembly模块. 这个<code>Module</code>能够再次被实例化 或 通过<a href="/en-US/docs/Web/API/Worker/postMessage">postMessage()</a>共享。</li> <li><code>instance</code>: {{jsxref("WebAssembly.Instance")}} 对象包含WebAssembly所有公开方法 <a href="/en-US/docs/WebAssembly/Exported_functions">Exported WebAssembly functions</a>.</li> </ul> <h3 id="Exceptions">Exceptions</h3> <ul> <li>如果任意参数的类型或结构错误, {{jsxref("TypeError")}} 抛出.</li> <li>如果操作失败, Promise通过reject根据失败原因返回 {{jsxref("WebAssembly.CompileError")}},{{jsxref("WebAssembly.LinkError")}} 或 {{jsxref("WebAssembly.RuntimeError")}}。</li> </ul> <h2 id="Examples">Examples</h2> <p>下面的示例 (在GitHub上查看 <a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/instantiate-streaming.html">instantiate-streaming.html</a> 示例, 并且也可 <a href="https://mdn.github.io/webassembly-examples/js-api-examples/instantiate-streaming.html">view it live</a> ) 直接从基础源传输一个 .wasm 模块,然后进行编译和实例化, Promise 履行后返回一个 <code>ResultObject</code>. 因为 <code>instantiateStreaming()</code> 方法允许履行后返回{{domxref("Response")}}对象的Promise,你可以直接传递一个 {{domxref("WindowOrWorkerGlobalScope.fetch()")}} 请求,它会在履行后将response传递给方法.</p> <pre class="brush: js">var importObject = { imports: { imported_func: arg => console.log(arg) } }; WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject) .then(obj => obj.instance.exports.exported_func());</pre> <p>然后访问<code>ResultObject</code>的实例成员,并调用包含的公开函数。</p> <h2 id="Specifications">Specifications</h2> <table class="standard-table"> <thead> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('WebAssembly Embedding', '#webassemblyinstantiatestreaming', 'instantiateStreaming()')}}</td> <td>{{Spec2('WebAssembly Embedding')}}</td> <td>Initial draft definition.</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> {{Compat}} <h2 id="See_also">See also</h2> <ul> <li><a href="/en-US/docs/WebAssembly">WebAssembly</a> overview page</li> <li><a href="/en-US/docs/WebAssembly/Concepts">WebAssembly concepts</a></li> <li><a href="/en-US/docs/WebAssembly/Using_the_JavaScript_API">Using the WebAssembly JavaScript API</a></li> <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></li> <li><a href="/zh-CN/docs/Web/API/Fetch_API/Using_Fetch">使用Fetch</a></li> </ul>