将 rest api promises 请求转换为 angular ionic 中的 rxjs 请求
Converting rest api promises requests to rxjs requests in angular ionic
我正在将旧项目的代码迁移到新项目中。在旧项目中,我对 rest api 请求使用 promises,但是,在新项目中,我使用 rxjs.
旧项目中剩余api请求方法的示例:
function getRequest(path, customHeader) {
var deferred = $q.defer();
var options = createOptions(deferred);
if (customHeader) {
options.headers = jsonConcat(options.headers, customHeader);
}
if (!isOnline) {
errorOccurred(null);
deferred.reject();
return deferred.promise;
}
var cachedData = serviceCache.getCachedNode(path);
if (cachedData) {
//console.log("Using cached data for request "+APP_CONSTANTS.GATEWAY.ENDPOINT+path);
//console.log(cachedData);
deferred.resolve(cachedData);
} else {
$http.get(APP_CONSTANTS.GATEWAY.ENDPOINT + path, options).then(
function(data, status, headers, config) {
if (serviceError(data)) {
deferred.reject(data);
} else {
deferred.resolve(data);
serviceCache.setCacheNode(path, data);
}
},
function(data, status, headers, config) {
errorOccurred(data);
deferred.reject(data);
}
);
}
return deferred.promise;
}
新项目中剩余api请求方法的示例:
getRequest(path, customHeader) {
let options = this.createOptions();
if (customHeader) {
options.headers = this.jsonConcat(options.headers, customHeader);
}
if (!this.isOnline) {
this.errorOccurred(null);
}
let cachedData = this.cacheService.getCachedNode(path);
if (cachedData) {
//console.log("Using cached data for request "+APP_CONSTANTS.GATEWAY.ENDPOINT+path);
//console.log(cachedData);
return cachedData;
} else {
return this.http
.get(
this.APP_CONSTANTS.getAppConstants().GATEWAY.ENDPOINT + path,
options
)
.pipe(
map(
(data) => {
if (this.serviceError(data)) {
return;
} else {
this.cacheService.setCacheNode(path, data);
}
},
(error) => {
this.errorOccurred(error);
}
)
);
}
}
我对延迟有点困惑。 defer 相当于什么,deferred.resolve(data);和 deferred.reject(数据);在 rxjs 中?我应该在我的新应用程序的示例代码中添加什么来代替延迟或者不需要添加任何东西?如果有人知道,请分享您的观点。谢谢
$q 本质上是 Promise 和 Angular 2+ 版本的实现,他们切换到 rxJs
并且都是关于 observable
。您可以根据需要处理可观察对象,它是一个数据流。
您可以查看的一些基本函数是 switchMap
、mergeMap
、combineLatest
等
我正在将旧项目的代码迁移到新项目中。在旧项目中,我对 rest api 请求使用 promises,但是,在新项目中,我使用 rxjs.
旧项目中剩余api请求方法的示例:
function getRequest(path, customHeader) {
var deferred = $q.defer();
var options = createOptions(deferred);
if (customHeader) {
options.headers = jsonConcat(options.headers, customHeader);
}
if (!isOnline) {
errorOccurred(null);
deferred.reject();
return deferred.promise;
}
var cachedData = serviceCache.getCachedNode(path);
if (cachedData) {
//console.log("Using cached data for request "+APP_CONSTANTS.GATEWAY.ENDPOINT+path);
//console.log(cachedData);
deferred.resolve(cachedData);
} else {
$http.get(APP_CONSTANTS.GATEWAY.ENDPOINT + path, options).then(
function(data, status, headers, config) {
if (serviceError(data)) {
deferred.reject(data);
} else {
deferred.resolve(data);
serviceCache.setCacheNode(path, data);
}
},
function(data, status, headers, config) {
errorOccurred(data);
deferred.reject(data);
}
);
}
return deferred.promise;
}
新项目中剩余api请求方法的示例:
getRequest(path, customHeader) {
let options = this.createOptions();
if (customHeader) {
options.headers = this.jsonConcat(options.headers, customHeader);
}
if (!this.isOnline) {
this.errorOccurred(null);
}
let cachedData = this.cacheService.getCachedNode(path);
if (cachedData) {
//console.log("Using cached data for request "+APP_CONSTANTS.GATEWAY.ENDPOINT+path);
//console.log(cachedData);
return cachedData;
} else {
return this.http
.get(
this.APP_CONSTANTS.getAppConstants().GATEWAY.ENDPOINT + path,
options
)
.pipe(
map(
(data) => {
if (this.serviceError(data)) {
return;
} else {
this.cacheService.setCacheNode(path, data);
}
},
(error) => {
this.errorOccurred(error);
}
)
);
}
}
我对延迟有点困惑。 defer 相当于什么,deferred.resolve(data);和 deferred.reject(数据);在 rxjs 中?我应该在我的新应用程序的示例代码中添加什么来代替延迟或者不需要添加任何东西?如果有人知道,请分享您的观点。谢谢
$q 本质上是 Promise 和 Angular 2+ 版本的实现,他们切换到 rxJs
并且都是关于 observable
。您可以根据需要处理可观察对象,它是一个数据流。
您可以查看的一些基本函数是 switchMap
、mergeMap
、combineLatest
等