如果发生错误,如何正确终止中间件链?
在我的代码中
if (someProblem) {
res.status(statuscode).json({message: 'oops'});
return;
}
Run Code Online (Sandbox Code Playgroud)
然而,此后链中的中间件一直在打电话给我Can't set headers after they are sent错误。
是否有使用 vscode 在当前和最后选择的选项卡之间来回切换的快捷方式?就像我以前可以用 webstorm 做的那样。
我有一个ngOnInit做异步工作的方法。
expect异步工作完成后,如何执行语句?
it('test things once all the promises declared in ngOninit are resolved', () => {
comp.ngOnInit();
// I want to test that comp.property is like expected
});
Run Code Online (Sandbox Code Playgroud)
我的组件方法如下所示:
OnInit() {
this.asyncMethod();
};
asyncMethod() {
this.methodThatReturnsPromise().then(() => {
this.property = this.otherPropertyNowResolved;
})
};
Run Code Online (Sandbox Code Playgroud) 我distinctUntilChanged该如何处理这样的对象
myObs = Observable.from([{foo: 'bar'}, {foo: 'bar'}]);
myObs.distinctUntilChanged()
.subscribe(value => {
// I only want to receive {foo: 'bar'} once
});
Run Code Online (Sandbox Code Playgroud) 给出以下示例.是否有现场指定的简写
let myObj = {someField: 'someValue'}
let foo = 'value'
myObj.foo = foo // I would like to do the opposite of object destructuring: const {foo} = myObj. I don't want to repeat foo twice.
Run Code Online (Sandbox Code Playgroud) 我创建了一个withMemo函数,它返回所提供函数的记忆版本。
const memoizedFn = withMemo(fn)
Run Code Online (Sandbox Code Playgroud)
我怎样才能记住这个与递归一起使用的斐波那契函数?
const fibo = (n) => {
if (n <= 1) return 1
return fibo(n - 2) + fibo(n - 1)
}
Run Code Online (Sandbox Code Playgroud)
事实上并withMemo(fibo)没有提高性能,因为内部的递归调用fibo仍然指向未记忆的版本......
所以我必须改变 fibo 的声明才能使 momoization 工作:
const momoizableFibo = memoizer => {
const fibo = (n) => {
if (n <= 1) return 1
return memoizer(fibo)(n - 2) + memoizer(fibo)(n - 1)
}
return memoizer(fibo)
}
// momoizableFibo(withMemo)(50) // takes a ms
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以记忆fibo(或任何其他递归函数)而不像我那样改变它的声明?
当我收到某个值时,如何取消订阅可观察值?
像这样 :
let tempSub: Subscription = this.observable$.subscribe(value => {
if (value === 'somethingSpecific') {
tempSub.unsubscribe();
// doesn't work
//because when this is reached tempsub is undefined
}
});
Run Code Online (Sandbox Code Playgroud) javascript ×4
rxjs ×2
algorithm ×1
angular ×1
ecmascript-6 ×1
express ×1
memoization ×1
node.js ×1
promise ×1
recursion ×1
typescript ×1