在Ramda中是否有一个'finally‘实现,用于进行函数组合,并调用一个函数,而不管promise的结果如何?我想做这样的事情:
compose(
finally(() => console.log("The promise resolved or rejected, who cares!"))
fetchAsync(id)
)如果不是,我正在考虑做一些这样的事情:
const finally = fn => compose(
otherwise(() => fn()),
andThen(() => fn())
);对此有什么想法吗?
发布于 2020-09-29 16:17:53
无论结果如何,otherwise之后的andThen调用都会被调用,并且可以充当finally
compose(
andThen(() => console.log("The promise resolved or rejected, who cares!")),
otherwise(() => console.log('failed')),
fetchAsync(id)
)发布于 2020-10-05 23:28:53
我不认为otherwise在这种情况下是足够的,因为它只被称为on-failure。相比之下,无论如何,.finally都会被调用-
const effect = f => x =>
(f(x), x)
const log = label =>
effect(x => console.log(label, x))
const err = label =>
effect(x => console.error(label, x))
const test = p =>
p
.then(log("resolved"))
.catch(err("rejected"))
.finally(log("you will ALWAYS see this"))
test(Promise.resolve(1))
test(Promise.reject(2))
resolved 1
Error: rejected 2
you will ALWAYS see this undefined
you will ALWAYS see this undefined我不确定Ramda的库中是否有这样的函数。如果不是,它很容易实现-
const andFinally =
curry(function(f, p){ return p.finally(f) })https://stackoverflow.com/questions/64115025
复制相似问题