赞
踩
我正在学习角度和打字稿.
我在这个服务中有一个CustomerService我有一个方法,我希望从RESTfull服务返回一组客户.
最初我创建了我的GetCustomers函数:
public GetCustomers(): Dtos.ICustomer[] { var _customers: Dtos.ICustomer[]; this._httpService.get('http://localhost/myTestApi/api/customers/') .success(function (data) { _customers = data as Dtos.ICustomer[]; }).error(function (error) { console.log(error); }); return _customers; }
这个函数最终得到了客户,但显然它会在httpservice实际获取数据之前返回_customers.
此时我以为我可以使用Typscript async/await,这就是我在一团糟中结束的时候.
我想写这样的函数:
public async GetCustomers(): Dtos.ICustomer[] { var _customers: Dtos.ICustomer[]; await this._httpService.get('http://localhost/myTestApi/api/customers/') .success(function (data) { _customers = data as Dtos.ICustomer[]; }).error(function (error) { console.log(error); }); return _customers; }
我立即得到此错误:错误TS1055类型'Dtos.ICustomer []'不是有效的异步函数返回类型.
我找到了这个Async/Await,简单的例子(typescript)
但它使用Promise对象:返回新的Promise
如果我尝试重写我的GetCustomers方法签名:
public async GetCustomers(): Promise{}
我得到并且错误:
找不到名字'承诺'
我需要导入一些东西来获得承诺吗?
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。