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

  • fireflame65

    fireflame65

    27 Mart 2007
  • LiquidMusick

    LiquidMusick

    23 Aralık 2010
  • Showtime

    Showtime

    21 HAZİRAN 2006