我无法理解thenApply(和之间的区别thenCompose().
那么,有人可以提供有效的用例吗?
来自Java文档:
thenApply(Function<? super T,? extends U> fn)
Run Code Online (Sandbox Code Playgroud)
返回一个新的
CompletionStage,当该阶段正常完成时,将使用此阶段的结果作为所提供函数的参数执行.
thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
Run Code Online (Sandbox Code Playgroud)
返回一个新的
CompletionStage,当此阶段正常完成时,将使用此阶段作为所提供函数的参数执行.
我得到的第二个参数thenCompose扩展了CompletionStage,thenApply而不是.
有人可以提供一个例子,我必须使用thenApply何时使用thenCompose?
我想知道在Angular 5中加载数据的最佳方法是什么.当然,我们希望保持组件哑;;现在,我正在使用一个解析器,Angular在特定路径中调用解析器.基本上,数据在组件初始化之前加载,因此我可以向用户显示加载器动画.
例如.
AResolver.resolver.ts
@Injectable()
export class AResolver implements Resolve<any> {
constructor(private readonly aService: AService) {}
resolve(route: ActivatedRouteSnapshot) {
return this.aService.find(route.paramMap.get('id'));
}
}
Run Code Online (Sandbox Code Playgroud)
M.module.ts
export const MRoutes: Routes = [
{
path: 'route-x',
component: AComponent,
resolve: AResolver
}
];
Run Code Online (Sandbox Code Playgroud)
AComponent.component.ts
@Component({
selector: 'a-super-fancy-name',
})
export class AComponent {
constructor(private readonly route: ActivatedRoute) {}
}
Run Code Online (Sandbox Code Playgroud)
好吧,到目前为止一切顺利,但是:
例
@Injectable()
export class ABResolver implements Resolve<any> {
constructor(private readonly aService: AService, private readonly bService: BService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.aService.resolve(route, …Run Code Online (Sandbox Code Playgroud) 我在Google上搜索了很多但是找不到我要找的地方:
如果未定义Property,则使用Object.hasOwnProperty与测试的好处
如何确定Native JavaScript Object是否具有属性/方法?
..以及很多其他网站,但这不是我要找的地方.我真正的问题是:
为什么hasOwnProperty在他的超类(原型)中找不到方法?为什么有人甚至会使用hasOwnProperty?它比typeof它慢得多,如果你正在使用继承它不起作用.
..第二个问题:
在这个问题中,Barney回答你必须用来if ('property' in objectVar)检查属性是否存在,但不解释原因.有人知道你为什么要使用这种结构吗?
var objA = function(){};
objA.prototype.alertMessage = function(){
return 'Hello A';
};
var objB = function(){
this.hello = function(){
return 'hello';
};
};
// Inheritance
objB.prototype = Object.create(objA.prototype);
objB.prototype.constructor = objA;
var test = new objB();
if (test.hasOwnProperty("alertMessage")){
console.log("hasOwnProperty: " + test.alertMessage());
}
if (typeof test.alertMessage === "function"){
console.log("typeof: " + test.alertMessage());
}
if (test.hasOwnProperty("hello")){
console.log("hasOwnProperty: " …Run Code Online (Sandbox Code Playgroud)