2015-06-06 54 views
0

我在使用Javascript的異步函數時遇到困難。 下面顯示的代碼到目前爲止幾乎是我擁有的全部代碼,我無法使其工作。jQuery異步問題undefined值

我試圖使用Eventful API從服務器中提取一些數據,並將其顯示在由jQuery創建的我的前端中。

所以,問題如下:功能搜索,這是調用函數Eventful.prototype.searchanje總是不確定的值結束了,而幾秒鐘後,功能searchanje控制檯登錄實際/收到的數據。

我對jQuery相當陌生,我想知道是否有任何一種「模板」來處理這些事情,具體來說就是等待函數返回值,然後繼續下一步操作。 到目前爲止,我已經嘗試使用延遲和承諾,並閱讀了相當多的教程和類似的科目stackoverflow答案,但我不能讓它正常工作。 或者,如果推遲和承諾是正確的路要走,你能告訴我它應該完成的方式嗎?

在此先感謝

'use strict'; 

function Eventful(_name) { 
var name = _name; 
var appKey = 'appKey'; 
var that = this; 

return { 
    getName: function() { 
     return name; 
    }, 
    getAppKey: function() { 
     return appKey; 
    }, 
    search: function() { 
     that.searchanje(appKey).then(function(oData) { 
      console.log('oData'); 
     }); 
    } 
}; 
} 

Eventful.prototype.searchanje = function(appKey) { 
var oArgs = { 
    app_key: appKey, 
    q: 'sport', 
    where: 'Zagreb', 
    date: '2013061000-2015062000', 
    page_size: 5, 
    sort_order: 'popularity', 
}; 

EVDB.API.call('/events/search', oArgs, function(oData) { 
    console.log(oData); 
    return oData(); 
}); 
}; 
+1

的可能重複[如何返回從異步調用的響應?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-打電話) –

回答

0

上線

EVDB.API.call('/events/search', oArgs, function(oData) { 

您傳遞一個回調函數。該功能(即啓動function(oData)的功能)不會立即執行。當API調用的結果返回時,它將異步執行。

嘗試將console.log()語句放在代碼中,並觀察它們在控制檯中的顯示順序。例如:

function Eventful(_name) { 
    var name = _name; 
    var appKey = 'appKey'; 
    var that = this; 

    return { 
     getName: function() { 
      return name; 
     }, 
     getAppKey: function() { 
      return appKey; 
     }, 
     search: function() { 
      console.log('Search function called'); 
      that.searchanje(appKey).then(function(oData) { 
       console.log('searchanje returned with data:'); 
       console.log('oData'); 
      }); 
     } 
    }; 
} 

Eventful.prototype.searchanje = function(appKey) { 
    console.log('function searchanje being called with appKey: ', appKey); 
    var oArgs = { 
     app_key: appKey, 
     q: 'sport', 
     where: 'Zagreb', 
     date: '2013061000-2015062000', 
     page_size: 5, 
     sort_order: 'popularity', 
    }; 

    console.log('Calling EVDB.API.call'); 
    EVDB.API.call('/events/search', oArgs, function(oData) { 
     console.log('EVDB.API callback executing with data:'); 
     console.log(oData); 
     return oData(); 
    }); 
    console.log('finished calling EVDB.API.call'); 
}; 
+0

好的,你有什麼建議該怎麼做,從這裏繼續?如何等待該返回值,然後繼續執行? – Zorkan

+0

@Zorkan是啊,你在回調函數 –

+0

裏面用你的結果做「東西」是不是在回調地獄結束的方式?不是具體地爲這些幾行代碼在這裏起牀,而是爲了更復雜的項目? – Zorkan

0

我想知道是否有更好的方法可能是「promisify」EVDB.API.call()

(function(app_key) { 
    var appKeyObj = { 'app_key': app_key }, 
    EVDB.API.callAsync = function(path, params) { 
     return $.Deferred(function(dfrd) { 
      EVDB.API.call(path, $.extend(appKeyObj, params), function(oData) { 
       if (oData.error === "1") { 
        //translate oData.status and oData.description into a javascript Error object 
        // with standard .name and .message properties. 
        err = new Error(oData.description); 
        err.name = oData.status; 
        dfrd.reject(err); 
       } else { 
        dfrd.resolve(oData); 
       } 
      }); 
     }); 
    }); 
})('myAppKey'); //hard-coded app key 

注:

  • 使用自執行的匿名包裝
  • 延長EVDB.API命名空間:

    • 全局命名空間是可以避免的。
  • 的「異步」後綴的promisified方法在青鳥
  • 一個precendent從小事我明白EVDB.APIEVDB.API.call()不只是一切,因此,單一的方法EVDB.API.callAsync()。如果有必要,可以添加更多的異步方法到EVDB.API
  • 您可能會選擇使用專用的promise庫,例如bluebirdwhen.js來代替jQuery。除了包含lib之外,上述代碼的mods會很小。

現在,不要致電EVDB.API.call(path, params, callback),您會撥打EVDB.API.callAsync(path, params)返回Promise。

var params = { 
    //app_key will be inserted automatically 
    q: 'sport', 
    where: 'Zagreb', 
    date: '2013061000-2015062000', 
    page_size: 5, 
    sort_order: 'popularity' 
}; 
EVDB.API.callAsync('/events/search', params).then(function(oData) { 
    console.log(oData); 
}, function(err) { 
    console.error(err); 
});