我碰上这种飘飞:的Javascript模式完成承诺
return somethingThatReturnsAPromise()
.then((response) => {
soSomethingg(); // Eg; update the UI
return response;
});
现在我正在寻找的东西预计不会返回任何东西,也不会改变的诺言链,如果我忘记即:
return somethingThatReturnsAPromise()
.whatImLookingFor((response) => {
doSomething(); // Eg; update the UI
})
.then((response) => {
// and this one should still be able to access response
});
也许这违背承诺的想法,但对我来说,因为我无法通过任意函数这是一个有点不方便。
一个想法是组成一个功能:
const sideEffect = (callback) => {
return (response) => {
callback(response);
return response;
};
};
而且我可以用它作为
return somethingThatReturnsAPromise()
.then(sideEffect(doSomething));
但我喜欢的东西,而不是then
有类似的东西?
注意:我正在使用Angular 1.x,所以我需要类似的东西。
'sideEffect'是一个完美的解决方案。你不喜欢它什么? – Bergi