简体   繁体   English

响应中带有状态对象的NodeJS http.get

[英]NodeJS http.get with state object in the response

I am struggeling with something that I belive supposed to be quite simple once you know how to do it , I apologize if its already been answered - I have searched throughly and could not find an answer. 一旦您知道该怎么做,我就会为之以为应该是一件很简单的事情,我为之感到困惑,如果它已经被回答,我深表歉意。-我经过彻底搜索,找不到答案。

In Node JS , I want to perform http.get , in the response parameter, I dont pass a closure but a reference to a function that receive a response in a parameter (that way I can have re usability) but I also want to have a state/data object that will be passed to this response function so I could further handle the transaction - with closures its easy but then I need to copy/paste all the time. 在Node JS中,我想在响应参数中执行http.get,我没有传递闭包,而是引用了在参数中接收响应的函数的引用(这样我就可以具有可重用性),但是我也想拥有一个状态/数据对象,该对象将传递给此响应函数,这样我就可以进一步处理事务-轻松完成关闭操作,然后我需要一直复制/粘贴操作。

 https.get({
        host: 'www.****.com',
        path: ********
    }, this._searchResponseHandler.bind(this));

How can that pleased be achieved ? 怎样才能实现呢?

Thank you , 谢谢 ,

James 詹姆士

You can use bind() to also provide initial arguments to your function (the result of which are called partial functions ): 您还可以使用bind()为函数提供初始参数(其结果称为部分函数 ):

this._searchResponseHandler.bind(this, stateObj)

The signature of the handler would become: 处理程序的签名将变为:

_searchResponseHandler : function(stateObj, res) { ... }

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

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