如何拦截承诺请求?(How to intercept promise request?)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
如何拦截承诺请求?(How to intercept promise request?)

我正在使用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.

更多推荐

本文发布于:2023-04-24 21:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/61e90ea95bd90ebba2c2bf87c8981b06.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:intercept   promise   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!