0
如何從異步函數中獲取變量?如何從aysnc函數中獲取變量
我有以下,我想從這個異步函數獲取httpsResp變量。
var httpsResp;
var dfd = this.async(10000);
var httpsReq = https.request(httpOptions, dfd.callback(function (resp) {
httpsResp = resp.statusCode;
assert.strictEqual(httpsResp, correctResp, error.incorrectResp);
}), dfd.reject.bind(dfd));
httpsReq.end();
httpsReq.on('error', function(e) {
console.error(e);
});
console.info('Status Code: ' + httpsResp);
當前,httpsResp顯示未定義。
[爲什麼我的變量在函數內部修改後沒有改變? - 異步代碼引用](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Barmar