我正在使用q promises,我想在承诺开始时展示微调器。 目前我这样做:
getPromise().then(function() { spinner.hide() })在getPromise() fn中,我正在显示微调器,所以getPromise看起来像:
function getPromise() { spinner.show() }但有没有办法拦截q中的then块,以便我可以将spinner.show添加到该拦截?
I'm using q promises, and I want to show the spinners when promise starts. Currently I'm doing this way:
getPromise().then(function() { spinner.hide() })and in the getPromise() fn, I'm showing up the spinner, so getPromise looks like:
function getPromise() { spinner.show() }But is there any way to intercept the then block in q, so that I can add the spinner.show to that intercept?
最满意答案
你是在考虑它
var spinOnPromise = function(p) { spinner.show() p.finally(function() { spinner.hide() }); return p; }传递承诺,只要承诺未决,微调器就会消失。
编辑:你可以这样做:
var spinOnPromise = function(p) { spinner.show() return p.finally(function() { return spinner.hide() }); }如果你这样做,区别在于,如果spinner.hide()返回一个promise(称之为p1 ),从spinOnPromise()返回的spinOnPromise()将不会被解析,直到p1被解析,但它将解析为与p 。 详情请见此处 。
你可以做到这一点,但我没有看到为什么你会这样做。
You are over-thinking it
var spinOnPromise = function(p) { spinner.show() p.finally(function() { spinner.hide() }); return p; }Pass in the promise, and the spinner will go as long as the promise is pending.
Edit: you could do this:
var spinOnPromise = function(p) { spinner.show() return p.finally(function() { return spinner.hide() }); }If you do this this, the difference is, if spinner.hide() returns a promise (call it p1), the promise returned from spinOnPromise() will not be resolved until p1 is resolved, but it will resolve to the same value as p. See here for details.
You could do this, but I don't see offhand why you would.
更多推荐
发布评论