[英]Access local variable in promise then scope
我想在then
使用bind
的上下文中訪問局部變量x
,但我得到的只是x
是不確定的。 我正在使用Promise
來檢索數據。
Ajax調用通過以下函數發生:
function ajaxCall(){
p = new Promise(function(resolve, reject){
$.ajax({
url: 'http://localhost:9090/bru/struc/arrivals',
headers: { 'Authorization' : "Basic " + btoa("BRU:BRU")},
type: "GET",
success: function(data, status, xhr) {
resolve(data)
},
error: function(xhr, textStatus, errorThrown) {
xhr = reject
}
})
})
}
在這里我想解開promise並將其值分配給局部變量x
function getColumns(){
var x
r = ajaxCall().then(function(result){
this.x.val = result \\result is available here, I can see it
console.log(result)
}.bind(this))
console.log('Columns retrieved.')
return x
}
嘗試一下。 done
而不是。 then
function getColumns(){
var x
r = ajaxCall().done(function(result){
this.x.val = result \\result is available here, I can see it
console.log(result)
}.bind(this))
console.log('Columns retrieved.')
return x
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.