English 中文(简体)
• 如何从诺德的职能中恢复身份法。 j)
原标题:How to return status code from function in Node.js

我有这样的职能:

function getStatusCode(site){
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
                    Host: site
            }
        };

    var status;

    http.get(options, function(response) {
        status=response.statusCode;
    });
    return status;
} 

只有在http://www.c.o.org/www.un.org/ga/president

最佳回答

背后的整个想法。 j 属于非同步性质。 你们试图做的是,试图使设计变得nch笑,从而违反了设计模式。

由于get/code>的同步性质 不能这样做。 你只能在警示职能范围内操纵地位法。 因此, /code>就不需要退还任何款项。 您可以另外计算一个参数,在取得结果后,即可援引这一参数:

function getStatusCode(site, callback) {
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
            Host: site
        }
    };

    http.get(options, function(response) {
        callback(response.statusCode);
    });
}

之后:

getStatusCode( http://... , function(statusCode) {
    alert(statusCode);
});
问题回答

拟议办法的规模并不大,特别是如果你需要处理错误的话:在复杂情况下,你会得到伊斯兰堡的传教。 因此,上述例子应作为入门级辅导处理,在生产中使用 should。

您可以使用<代码>return<>/code>,按顺序返还价值,但您不应再回馈价值,而应退还某种延续价值。 未来和许诺就是这种延续的一个例子。 看看提供这种服务的国家预防机制图书馆。 例如,见qq-http://www.un.org/Depts/DGACM/index_spanish.htm>。

另见以下相关问题:

《共同法》中的承诺摘要有什么好处?

Understanding promises in node.js

我想q 图书馆因其有姊妹图书馆(q-io,q-http)执行大多数标准节点。

事实上,一个早期标准图书馆用于兑现诺言,但后来决定使用反馈。 这不是因为允诺不好,而是因为呼吁程度较低。

你们可以自行决定“恢复”,因为这需要一(一)项职能,而这是没有的。 相反,你可以通过背书功能,这种功能将用你的地位法典来命名。

function getStatusCode(site, callback){
    var options = {
        host: "127.0.0.1",
        port: 8000,
        path: site,
        headers: {
            Host: site
            }
        };

    var status;

    http.get(options, function(response) {
        status=response.statusCode;
        callback(status);
    });
} 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.