简体   繁体   English

通过承诺链传递数据(angularjs)

[英]Passing Data through Promise Chain (angularjs)

The question is hard to describe so let me post an example. 这个问题很难描述,所以让我举个例子。 The context is AngularJS $q , but any solutions based on ES6 Promise are also welcome. 上下文是AngularJS $q ,但是也欢迎任何基于ES6 Promise解决方案。

Suppose we want to cancel an $http request. 假设我们要取消$http请求。 In AngularJS this is implemented by providing a promise A to $http.get() method. 在AngularJS中,这是通过向$http.get()方法提供promise A来实现的。 Resolve promise A will result in cancelling the request. 解决承诺A将导致取消请求。 So the code will look like this: 因此,代码将如下所示:

function httpCall() {
    const promiseA = $q.defer();
    const promiseB = $http.get(url, { timeout: promiseA.promise });
    return promiseB;
}

In order to return promiseA so that the caller of httpCall can cancel the request, the only way I found is to assign it to promiseB , like this: 为了回报promiseA ,这样的调用者httpCall可以取消该请求,我发现的唯一办法就是将它分配给promiseB ,就像这样:

function httpCall() {
    const promiseA = $q.defer();
    let promiseB = $http.get(url, { timeout: promiseA });
    promiseB.canceller = promiseA;
    return promiseB;
}

The problem is, promiseB may be passed through a long promise chain (such as action dispatch, mapping, other post-processes), for example: 问题是, promiseB可能会通过很长的promise链传递(例如,动作分派,映射,其他后处理),例如:

function doSomeAction() {
    return httpCall()
        .then((response) => {
            processResponse(response);
            return response;
        });
}

Obviously, the return value of doSomeAction is NOT the same promise as the origin promise promiseB . 显然, doSomeAction的返回值与原产地promiseB

Thus if I have a function who calls doSomeAction() , this function will not be able to retrieve promiseB (it can only get the return promise of doSomeAction() ), thus not able to cancel the request. 因此,如果我有一个调用doSomeAction()函数,则此函数将无法检索promiseB (它只能获取doSomeAction()的返回承诺),因此无法取消该请求。

Someone may say we can pass the data ( promiseA ) through the chain. 有人可能说我们可以通过链传递数据( promiseA )。 However I consider the existance of promiseA should be transparent to the chain itself, ie the functions in the chain should never know the existance of promiseA . 但是我认为promiseA的存在对链本身应该是透明的,即链中的功能永远不应该知道promiseA的存在。 Thus I cannot depend on the chain to pass data for me. 因此,我不能依靠链条为我传递数据。

Any suggestions are welcome. 欢迎任何建议。

Not knowing about promiseA makes total sense as things are happening on the different levels of abstraction. 由于事情在不同的抽象级别上发生,因此不了解promiseA完全有意义。 doSomeAction abstracts out http call and providing any API which cancels underlying http call breaks an abstraction and reveal doSomeAction 's implementation details. doSomeAction抽象出http调用,并提供任何取消基础http调用的API都会破坏抽象并显示doSomeAction的实现细节。 Instead I would think about making doSomeAction cancellable ie providing a cancel method on an object it returns. 相反,我会考虑使doSomeAction可取消,即在它返回的对象上提供一个cancel方法。 This cancel method can cancel underlying http request (or several of them if there are several) or skip cancelling if no http request was made and data was taken from local storage or do whatever makes sense for doSomeAction , not just particular http request which is just part of a bigger picture on this layer. 这种cancel方法可以取消基础的http请求(如果有多个,则取消其中的几个请求),或者如果没有发出http请求并且从本地存储中获取数据,或者跳过对doSomeAction有意义的doSomeAction ,则跳过取消操作在这一层更大的图片的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM