2014-04-02 20 views
0

我有我现在使用两种方法,第一种是检查一个网站,或根本没有,通过检查HTTP GET状态代码响应:如何检查网站是否异步启动?

checkifUp = (host, callback) -> 
    host = host.replace("http://", "").replace("/", "") 
    options = 
    host: "#{host}" 
    port: 80 
    path: "/" 

    req = http.get(options, (res) -> 
    callback(res.statusCode.toString()) 
    req.connection.destroy() 
) 

的Javascript:http://goo.gl/OyekSx

并与第二个,我做的是从网站我有一个数组选择随机网站和验证(与第一种方法的帮助下),如果这样的网站了。如果是这样,我想返回该网站作为父方法的返回值(在本例中是第二个),但如果不是,我想重新运行该方法,直到它找到数组中的在线网站。这是我迄今所做的:

siteSet = -> 
    site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com", "sub4.mysite.com", "sub5.mysite.com"] 
    random_site = site_urls[Math.floor(Math.random()*site_urls.length)] 

    checkifUp "#{random_site}", (code) -> 
    if code isnt "200" 
     siteSet() 
    else 
     selected_site = random_site 
    selected_site 

的Javascript:http://goo.gl/ydmSiV

显然,这不工作的方式我想:如果状态代码不是200,那么它确实重新运行该方法,(到目前为止,我们没事);但问题出现在网站确实在线时,因为我不知道如何返回selected_site变量(在checkifUp调用中声明)作为父方法的返回值(在这种情况下为siteSet() )。我需要这样做,以便能够使用siteSet()返回值作为另一个函数的变量:

otherFunc = -> 
    theSite = siteSet() 
    console.log(theSite) 

的Javascript:http://goo.gl/cmsryJ

而且有信心它总是会被放置在这个内的在线网站URL(字符串)otherFunc()

我有两个问题一下:

  1. 我怎么能做到我想做的事吗? (杜,一个很明显嘿嘿)

  2. 我不太清楚这一点,但据我了解 的JavaScript/CoffeeScript中,当siteSet()会从内 otherFunc称为() ,(至少在这个“setup”中),otherFunc()不会等到siteSet()返回一个String(这是我想要的结果) 我是正确的吗?即使有回报的问题解决了,我认为是 将要发生的是,当我打电话siteSet()otherFunc()内将使用精确的结果从调用,这意味着如果当siteSet( )被拼命地跑,返回另一个呼叫 本身(因为random_site选择不在线)内otherFunc的 “theSite”变量()将采取裸()函数 的价值,我是不是正确的? (如果是这样的话),如何解决这个 其他问题?我想设置 otherFunc()里面的“theSite”变量,直到这样的值是一个字符串,因为我需要它。

在此先感谢您的帮助。

+0

你试过把回调传递给siteSet,和checkifUp一样吗?这就是异步混乱如何处理现在heh :) – ezakto

+0

你有3个回应,请投票选择更好的一个,或者如果你觉得没有得到很好的回应,请修复你的问题......谢谢.. – user1050817

回答

1

这里的问题是不是你”重新采取同步方法为您的其他功能,而不是异步,让我们来看看这个:

//sorry I prefer use plain js, I'm pretty sure than you will be able to understand the code 
    var funA = function(){ 
    //long computation here 
    console.log("calling from funA"); 

    } 

var funB = function(){ 
    var resultA = funA(); 
    console.log("resultA is " + resultA); 
    console.log("calling from funB"); 

    } 

    funB() 

结果会是这样的:

 resultA is undefined 
    calling from funB 
    calling from funA 

你的代码会翻译成这样:

//sorry I'm not so familiar with coffeescript so maybe I would do a little mistake 
    siteSet = (callback)-> 
    site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com",   "sub4.mysite.com", "sub5.mysite.com"] 
    random_site = site_urls[Math.floor(Math.random()*site_urls.length)] 

    checkifUp "#{random_site}", (code) -> 
     if code isnt "200" 
     siteSet() 
     else 
     selected_site = random_site 

     callback(selected_site) 


    otherFunc = -> 
     siteSet((result)-> console.log(result)) //(result)-> console.log(result) is your 
              //callback, so inside checkifUp you will call it and pass selected_site 
              // 

为更好地理解为什么执行的NodeJS这样的代码检查这些物品.. 。

http://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks

http://dreamerslab.com/blog/en/javascript-callbacks/

x = LONGIO() 
    console.log(x) 

    vs 

    LONGIO((resultOfLongIO)-> console.log(resultOfLongIO)) 

基本上在异步代码的想法(没有承诺,发电机,单子或其他)比你通过泛函给回调的结果...这是所有...

0

我不知道CoffeeScript,所以我使用JavaScript转换为您的两个功能。如果CoffeeScript中存在我不知道的特质,请告诉我。

也就是说,它看起来像你正在使用异步功能,siteSet,并试图同步返回一个值。您可以查看this question的解决方案以获取更多信息。您有有效的返回值的两个选项:

  • 活动
  • 回调

你有你的第一个功能,checkIfUp回调,所以我的建议是使用一个siteSet为好。你可以调用如下:

otherFunc = -> 
    theSite = siteSet (theSite) -> 
    console.log(theSite) 

至少,我认为这是CoffeeScript语法。在JavaScript它肯定:

otherFunc() 
{ 
    theSite = siteSet(function(theSite) 
    { 
    console.log(theSite); 
    }); 
} 
0

添加回调siteSet就像你与checkifUp做:

siteSet = (callback) -> 
    site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com", "sub4.mysite.com", "sub5.mysite.com"] 
    random_site = site_urls[Math.floor(Math.random()*site_urls.length)] 

    checkifUp "#{random_site}", (code) -> 
    if code isnt "200" 
     siteSet() 
    else 
     callback(random_site) 

然后,你可以这样做:

otherFunc = -> 
    siteSet (theSite) -> 
    console.log theSite