在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