0
我试图从打字稿函数返回一个字符串 -打字稿返回值
private _renderListAsync(): string {
let _accHtml: string='';
// Local environment
if (Environment.type === EnvironmentType.Local) {
this._getMockListData()
.then((response) => {
_accHtml = this._renderList(response.value);
alert("1: " + _accHtml)
})
alert("3: " + _accHtml);
return _accHtml;
}
else if (Environment.type == EnvironmentType.SharePoint ||
Environment.type == EnvironmentType.ClassicSharePoint) {
this._getListData()
.then((response) => {
_accHtml = this._renderList(response.value);
alert("2: " + _accHtml);
})
alert("3: " + _accHtml);
return _accHtml;
}
}
不过,我只能够得到的字符串值警报“1”和“2”,而不是“3 “警报是空的,因此我无法从函数返回_accHtml。我究竟做错了什么?另外我注意到在“1”和“2”之前出现“3”的警报,为什么?
我也尝试过在if-else块后返回_accHtml,仍然不起作用 – user3759018
可能重复[如何从异步调用返回响应?](http://stackoverflow.com/questions/14220321/how -do -i-return-the-an-asynchronous-call) – Andreas
我不能肯定地说,但是我看到promise API的一些指标(即'.then()',它可能表示你使用的是异步调用,这就像一个异步的问题...... – War10ck