На данный момент я изучаю Angular2-alpha45.
В связи с проблемой CORS я должен сделать вызов JSONP. Проблема в том, что вызов занимает некоторое время, и я не знаю, как обернуть ответ в обещание.
Я могу обернуть обычный http.get в обещание, но из-за CORS это не решение для моих нужд.
Рабочий пример http.get:
import {Jsonp, Http} from 'angular2/http';
// works
this.getPromise().then(result => {
console.dir(result)
});
getPromise(): Promise<Array> {
return this.http
.get('test.json')
.map((res) => {
return res.json()
})
.toPromise();
}
Не работает
import {Jsonp, Http} from 'angular2/http';
// Doesn't work
this.getPromiseJsonp().then(result => {
console.dir(result)
});
getPromiseJsonp(): Promise<Array> {
// this.generateJsonpUrlDataGet2 generates the URL for call, URL is correct
// response is sent but without a promise
var url = this.generateJsonpUrlDataGet2('SingleUser', "test", '');
return this.jsonp.request(url).subscribe(res => {
// console.dir() get called!
console.dir(res._body);
return res._body;
}).toPromise();
}
Может ли кто-нибудь сказать мне, как обернуть вызов Jsonp в обещание?
this.jsonp.request(url).toPromise().then(() => ...)
, хотя это точно то же самое, что вы делаете сsubscribe
. - person Eric Martinez   schedule 12.11.2015