javascript - Node.js/Express.js - 我应该将我所有的函数包装在一个新的 Promise 中吗?

标签 javascript node.js express

express 文件Production best practices: performance and reliability说:

Don’t use synchronous functions

Synchronous functions and methods tie up the executing process until they return. A single call to a synchronous function might return in a few microseconds or milliseconds, however in high-traffic websites, these calls add up and reduce the performance of the app. Avoid their use in production.

所以我的问题是,在 node/express 的上下文中,如果我有一个接受一些静态值并返回计算结果的函数(我通常认为是“同步函数”),最好的做法是包装new Promise 中的那个函数和 resolve 结果,或者这是否会产生任何不必要的重大开销?例如:

当前:

//inside my index.js
var myArgument = 'some long string';
var myResult = myFunction(myArgument);

function myFunction(myArgument){
  var thisResult;
  //some calculations
  return thisResult;
}

新的(和改进的?)

//inside my index.js
(async function() {
var myArgument = 'some long string';
var myResult = await myFunction(myArgument);
});

function myFunction(url) {
  return new Promise((resolve, reject) => {
    var thisResult;
    //some calculations
    if(thisResult){
      resolve (thisResult);
    } else {
      reject (null)
    }
  });
}

最佳答案

简短的回答:不。

文档讨论的是不使用同步版本的函数,例如来自 nodeJS 文件系统的 readfileSync 或 bcrypt.compareSync。同步调用会阻塞 nodeJS 中的事件循环。因此,在您等待同步调用完成时什么也没有发生。当这个方法完成时,整个程序暂停。这在像 nodeJS 这样的单线程系统中是很糟糕的。

没有理由用回调或 promise 来包装只是简单计算或数组操作的函数。

它只是说,如果有一个库/方法提供该方法的同步版本,请尽量避免使用该方法。

checkout :https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/

JavaScript execution in Node.js is single threaded, so concurrency refers to the event loop's capacity to execute JavaScript callback functions after completing other work. Any code that is expected to run in a concurrent manner must allow the event loop to continue running as non-JavaScript operations, like I/O, are occurring.

As an example, let's consider a case where each request to a web server takes 50ms to complete and 45ms of that 50ms is database I/O that can be done asynchronously. Choosing non-blocking asynchronous operations frees up that 45ms per request to handle other requests. This is a significant difference in capacity just by choosing to use non-blocking methods instead of blocking methods.

The event loop is different than models in many other languages where additional threads may be created to handle concurrent work.

关于将所有内容包装在 promises 中的额外开销。答案仍然是否定的。

你不会体验到什么不同

function sum(x,y) {
  return x+y
}

const ans = sum(1,2)
console.log(ans) // 3

function sum(x,y) {
 return Promise.resolve(x+y) // Shorthand for your new Promise
}

sum(1,2).then(ans => {
  console.log(ans) //3
})

关于javascript - Node.js/Express.js - 我应该将我所有的函数包装在一个新的 Promise 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297132/

相关文章:

node.js - Jade - 将新行转换为 <br/> 并保持内容编码

node.js - 从 REST 调用返回对象

javascript - react 属性消失

javascript - Ember : Bubble action from component to application controller

javascript - .is (':visible' ) 在 jQuery 中不起作用

javascript - 带 Node 的 OpenVPN,它是如何工作的?

node.js - 以太坊 Node 中的 web3(然后是属性)

node.js - 如何在socket io中获取ip

node.js - 在 Elastic Beanstalk AWS 中为 NodeJs 应用程序提供静态文件

javascript - 如何在两个 div 之间建立关系,一个包含产品和其他选项