[英]Access local variable in promise then scope
I want to access a local variable x
in the context of then
using bind
but all I get is x
is undefined. 我想在
then
使用bind
的上下文中访问局部变量x
,但我得到的只是x
是不确定的。 I am using Promise
to retrieve data. 我正在使用
Promise
来检索数据。
The ajax call happens with this function: 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
}
})
})
}
And here I want to unwrap promise and assign its values to the local variable x
在这里我想解开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
}
try with . 尝试一下。
done
instead of . 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.