SORU
18 Mayıs 2011, ÇARŞAMBA


Nodejs senkron isteği

Eğer 3 http sırayla API aramam gerekiyorsa, ne aşağıdaki kodları daha iyi bir alternatif olurdu

http.get({ host: 'www.example.com', path: '/api_1.php' }, function(res) { 
  res.on('data', function(d) { 

    http.get({ host: 'www.example.com', path: '/api_2.php' }, function(res) { 
      res.on('data', function(d) { 

        http.get({ host: 'www.example.com', path: '/api_3.php' }, function(res) { 
          res.on('data', function(d) { 


          });
        });
        }
      });
    });
    }
  });
});
}

CEVAP
20 Mayıs 2011, Cuma


Raynos seviyorum ama farklı akış kontrolü bir kütüphane tercih ederim' çözüm gibi.

https://github.com/caolan/async

Her bir sonraki fonksiyon sonuçları ihtiyacınız olup olmadığını bağlı olarak, her iki seri, paralel veya şelale kullanmak istiyorum.

Seri olmak varken Series idam, ama mutlaka her bir sonraki fonksiyon çağrısında sonuçları ihtiyacın yok.

Parallel eğer paralel olarak yürütülen, her paralel işlev sırasında her sonuçları ihtiyacın yok, ve tüm tamamladıktan sonra geri gerekir.

Eğer her fonksiyonu sonuçları morph ve sonrakine geçmek istiyorsanız Waterfall

endpoints = 
 [{ host: 'www.example.com', path: '/api_1.php' },
  { host: 'www.example.com', path: '/api_2.php' },
  { host: 'www.example.com', path: '/api_3.php' }];

async.mapSeries(endpoints, http.get, function(results){
    // Array of results
});

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Bokeh

    Bokeh

    9 HAZİRAN 2014
  • Hey Nadine

    Hey Nadine

    24 Kasım 2006
  • International Secret Agents Concerts

    Internationa

    2 Aralık 2008