SORU
20 Kasım 2010, CUMARTESİ


Nasıl Node.js zaman uyumsuz fonksiyonların uzun yuvalama önlemek için

Bir DB bazı verileri görüntüleyen bir sayfa yapmak istiyorum, benim DB bu bilgileri nereden bazı fonksiyonları oluşturduk. Eğer hepsini yuva lazım, tek bir sayfa (HTTP response) hepsini kullanmak istiyorum eğer sadece anladığım kadarıyla yani node.js bir acemi değilim,:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  var html = "<h1>Demo page</h1>";
  getSomeDate(client, function(someData) {
    html  = "<p>"  someData  "</p>";
    getSomeOtherDate(client, function(someOtherData) {
      html  = "<p>"  someOtherData  "</p>";
      getMoreData(client, function(moreData) {
        html  = "<p>"  moreData  "</p>";
        res.write(html);
        res.end();
      });
    });
  });

Eğer bunun gibi birçok işlevi varsa, o zamaniç içe geçmiş bir sorun haline gelir.

Bunu önlemek için bir yolu var mı? Bir şeyin temel gibi birden çok zaman uyumsuz fonksiyonları, birleştirmek nasıl bir ilgisi var sanırım.

CEVAP
27 Kasım 2010, CUMARTESİ


Kay, sadece bu modülleri birini kullanın.

Bu dönecek:

dbGet('userIdOf:bobvance', function(userId) {
    dbSet('user:'   userId   ':email', 'bobvance@potato.egg', function() {
        dbSet('user:'   userId   ':firstName', 'Bob', function() {
            dbSet('user:'   userId   ':lastName', 'Vance', function() {
                okWeAreDone();
            });
        });
    });
});

Bu içine:

flow.exec(
    function() {
        dbGet('userIdOf:bobvance', this);

    },function(userId) {
        dbSet('user:'   userId   ':email', 'bobvance@potato.egg', this.MULTI());
        dbSet('user:'   userId   ':firstName', 'Bob', this.MULTI());
        dbSet('user:'   userId   ':lastName', 'Vance', this.MULTI());

    },function() {
        okWeAreDone()
    }
);

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 30GB

    30GB

    14 AĞUSTOS 2006
  • Andrew_Huang

    Andrew_Huang

    10 ŞUBAT 2006
  • 10 Daughters, 2 Sons

    10 Daughters

    10 Mart 2009