我正在构建一个小模态包,我想要一个流畅的 api,我想做的一件事是能够选择将动画函数传递到 close 方法中并调用该方法仅在可选动画完成后运行。
a) 我的想法正确吗?
b) 我应该只使用回调吗?
c) 有没有使用 setTimeout 的替代方法? (我不知道为什么这感觉很脏,但确实如此)
d) 我认为 Promise 是一个选项,但这会破坏语法。
我想要得到的是:
Modal.save().close().resetData();
最佳答案
a) I'm thinking about this correctly?
是的。
b) Should I just be using a callback?
是的,这种方法没有问题。
c) is there an alternative to using a setTimeout? (I don't know why this feels dirty but it does)
对于动画,请使用 CSS 过渡或 requestAnimationFrame
而不是 setTimeout
。
d) I suppose promises are an options but that would disrupt the syntax.
当你的 promise 起作用时,你可以重构它。 Promise 确实提供了更流畅的 API。
使用回调方法,我希望它看起来像这样:
function close(done) {
// do something async then call `done`
}
Modal.save(close.bind(Modal, resetData));
关于javascript - 在流畅的 Javascript API 中延迟函数的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121137/