87,987
社区成员
发帖
与我相关
我的任务
分享
function fnGetNameFromCoords(coord) {
var coords = coord.split(",")
var name = '';
map.getNameFromCoords({ //异步
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescriptio //name还没得及赋值
}
});
return name; //先执行到这里,name = ''
}
<text >{{=fnGetNameFromCoords(value.location)}}</text>
js:
async function fnGetNameFromCoords(coord) {
var coords = coord.split(",")
var name;
var promise = new Promise(resolve => {
map.getNameFromCoords({
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescription
resolve(name)
}
});
})
await promise;
return name;
}
[/quote]
我查了资料,使用了promise,的确是实现了异步操作的等待,但最后的结果是显示[object Promise],不知道哪里出了问题
渲染:
<text >{{=fnGetNameFromCoords(value.location)}}</text>
js:
async function fnGetNameFromCoords(coord) {
var coords = coord.split(",")
var name;
var promise = new Promise(resolve => {
map.getNameFromCoords({
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescription
resolve(name)
}
});
})
await promise;
return name;
}
[/quote]
最后调用一下fnGetNameFromCoords(coord)应该
function fnGetNameFromCoords(coord) {
var d=$.Deferred();
var coords = coord.split(",")
var name = '';
map.getNameFromCoords({ //异步
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescriptio //name还没得及赋值
d.resolve(name);
}
});
return d;
}
fnGetNameFromCoords('a,b').then(function(name){})
自己没权限编辑自己的回帖 是怕我抵赖呢? @床上等你
function fnGetNameFromCoords(coord) {
var d=$.Deferred();
var coords = coord.split(",")
var name = '';
map.getNameFromCoords({ //异步
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescriptio //name还没得及赋值
d.resolve(name);
}
});
}
fnGetNameFromCoords('a,b').then(function(name){})
async function fnGetNameFromCoords(coord) {
var coords = coord.split(",");
return new Promise((resolve,reject) => {
map.getNameFromCoords({
lon: coords[0],
lat: coords[1]
}, function(ret,err){
if(ret.status){
resolve(ret.sematicDescription)
} else {
reject(new Error('没有数据'));
}
});
})
}
// 使 doT 模板支持 await
function asyncDoT(strTmpl, args) {
let global = function(){ return this; }();
let F = global.Function;
global.Function = (async function(){}).constructor;
let tmpl = doT.compile(strTmpl);
global.Function = F;
return tmpl(args);
}
async function render() {
let tmpl = ` {{~it:value:index}} <text>{{= await fnGetNameFromCoords(value.location) }}</text> {{~}}`;
let data = [ {location:'1,2'},{location:'3,4'} ];
document.body.innerHTML = await asyncDoT(tmpl, data); // 渲染时也要 await
}
render();
async function ss(){
var p=new Promise(resolve=>setTimeout(resolve,1000,'a'));
return await p
}
ss().then(console.log);
<text >{{=fnGetNameFromCoords(value.location)}}</text>
js:
async function fnGetNameFromCoords(coord) {
var coords = coord.split(",")
var name;
var promise = new Promise(resolve => {
map.getNameFromCoords({
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescription
resolve(name)
}
});
})
await promise;
return name;
}
[/quote]
最后一步改成
return await promise试试<text >{{=fnGetNameFromCoords(value.location)}}</text>
js:
async function fnGetNameFromCoords(coord) {
var coords = coord.split(",")
var name;
var promise = new Promise(resolve => {
map.getNameFromCoords({
lon: coords[0],
lat: coords[1]
},function(ret,err){
if(ret.status){
name = ret.sematicDescription
resolve(name)
}
});
})
await promise;
return name;
}