在node.js
在諾言中並不固有地使它變得不受阻礙。 The Promise executor function is executed synchronously, meaning that long-running code within it will block the execution of other operations.
Example: Promise-Wrapped Blocking FunctionConsider the following function:function longRunningFunc(val, mod) { 返回新的承諾((分辨率,拒絕)=> { 令sum = 0; (讓i = 0; i 此函數返回承諾時,執行程序中的代碼正在阻止。事件循環將等到該代碼完成後才執行其他操作。
使用settimeout
此代碼在10毫秒延遲後將長期運行的循環計劃執行。但是,它仍在該延遲執行期內被阻止。真實的非阻止方法創建真正的非阻止功能,您需要使用將代碼在主node.js thread之外移動代碼執行的技術,而在node.js thread中移動代碼。處理長期運行的任務。 在node.js中使用實驗工作線程特徵在單個進程中創建多個線程。
現有的asynchronous apis:由Node.js核心模塊提供的Leveraver AsynChronous操作,例如使用fs.ReadFile,http requests http.get,或Database Queries,或Database Queries file i/o file i/o,與http。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3