简体   繁体   English

如何将此阻塞代码重构为对node.js非阻塞

[英]How to refactor this blocking code to non blocking for node.js

I need to refactor this code to non blocking. 我需要将此代码重构为非阻塞。

I have wrote a simple helper code, What this does is generates invoice Number based on provided previous number. 我已经编写了一个简单的帮助程序代码,它的作用是根据提供的先前编号生成发票编号。

helper.js helper.js

module.exports = {
  replaceCharAt: function replaceCharAt(string, index, character){
    var string = string.split('');
    string[index] = character;
    return string.join('');
  },
  generateInvoiceNumber: function generateInvoiceNumber(invoiceNumber, useIndex){
    var invoiceNumber = invoiceNumber.trim();
    if(typeof useIndex === 'undefined' || useIndex === -1 ){
      useIndex = invoiceNumber.length - 1;
    }
    var incrementChar = invoiceNumber[useIndex];
    if(incrementChar == '9' || incrementChar == 'z' || incrementChar == 'Z'){
      if(incrementChar == 9){
        invoiceNumber = this.replaceCharAt(invoiceNumber, useIndex, '0');
      }
      else{
        invoiceNumber = this.replaceCharAt(invoiceNumber, useIndex, 'a');
      }
      if(useIndex === 0){
        return invoiceNumber = '1'+invoiceNumber;
      }
      return this.generateInvoiceNumber(invoiceNumber, useIndex - 1);
    }
    else{
      var replaceChar = String.fromCharCode(incrementChar.charCodeAt(0) + 1);
      invoiceNumber = this.replaceCharAt(invoiceNumber, useIndex, replaceChar);
    }
    return invoiceNumber;
  }
};

How I use it in request handler. 我如何在请求处理程序中使用它。

var helper = require('./helper');

var handler = function(request, reply){
  var newNumber = helper.generateInvoiceNumber(request.params.oldNumber);
  reply({newNumber: newNumber});
}

So this code blocks until it generates the Invoice number. 因此,此代码将阻塞,直到生成发票编号。 I need some tips and hints on how to refactor this to not block. 我需要一些技巧和提示,以了解如何重构此功能以使其不受阻碍。

Since your code is all synchronous by nature, eg: you're not making any IO calls, there's no way for you to make this code async :( 由于您的代码本质上都是同步的,例如:您没有进行任何IO调用,因此您无法使该代码异步:(

Async code must be making IO calls, stuff like this: 异步代码必须正在进行IO调用,如下所示:

  • Writing to disk. 写入磁盘。
  • Talking over the network. 通过网络交谈。
  • Calling subprocesses and waiting for responses. 调用子流程并等待响应。

These things can all be made asynchronous because your program is simply waiting for some other piece of hardware to do something: process results, send responses, etc. 这些事情都可以异步进行,因为您的程序只是在等待其他硬件来做某事:处理结果,发送响应等。

In your code, you're simply performing some CPU operations: if statements, string comparisons, etc. This stuff always has to be processed by your CPU directly, and since that's the case, there's no way for you to 'wait for that to finish', so to speak. 在您的代码中,您只是在执行一些CPU操作:if语句,字符串比较等。这些东西始终必须直接由CPU处理,既然如此,您就无法“等待”完成”,可以这么说。

If you're really intent on making this async, you could do the following: 如果您确实打算进行异步处理,则可以执行以下操作:

  • Write an API service that performs those synchronous tasks. 编写执行这些同步任务的API服务。
  • Expose that API service over the network: http://blah.com/api 通过网络公开该API服务: http//blah.com/api
  • Rewrite your local Node code to make async calls to that API and wait for the results. 重写您的本地节点代码以对该API进行异步调用,然后等待结果。

The truth, however, is that since your code is pretty minimally synchronous, you shouldn't need to do the above. 然而,事实是,由于代码几乎是最小同步的,因此您不需要执行上述操作。 All projects have some synchronous code in them -- there's no escaping it =) 所有项目中都有一些同步代码-没有转义=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM