D3.js 在多次解析期间输出承诺值

D3.js 在多次解析期间输出承诺值,d3.js,promise,angular2-services,D3.js,Promise,Angular2 Services,我正在从d3.csv加载一个承诺,然后在随后的调用中对返回的数组进行多次更改 在每次更改之前,我需要将数组的状态输出到html(Angular 2),我可以使用(变量| async)执行此操作,但它会随着每次更改而更新,并且我需要输出每次更改之前的状态 我试图克隆这个承诺,但所有克隆都指向同一个承诺。任何变量都超出了更改的范围,并且无法访问父范围 data = d3.csv promise data.then(methodB()). // HTML | async Output Required

我正在从d3.csv加载一个承诺,然后在随后的调用中对返回的数组进行多次更改

在每次更改之前,我需要将数组的状态输出到html(Angular 2),我可以使用(变量| async)执行此操作,但它会随着每次更改而更新,并且我需要输出每次更改之前的状态

我试图克隆这个承诺,但所有克隆都指向同一个承诺。任何变量都超出了更改的范围,并且无法访问父范围

data = d3.csv promise
data.then(methodB()). // HTML | async Output Required of Array before changes
then(methodB()) // HTML | async Output Required of Array before changes
etc..
etc..
etc..
(There are around 15 methods applied to the data as it is munched and analyzed)
实现这一目标的最佳方法是什么?

假设:

  • 您有一个名为
    csvPromise
    的初始承诺,它提供了阵列
  • 要应用的方法有
    methodA
    methodB
    methodC
    等,每个方法都接受数组
  • 每个方法要么返回输入数组的变异,要么通过承诺传递变异
  • 对数组的更改是累积的,方法对方法
  • 您有一个同步函数
    output()
    ,它将接受原始数组和每个变异
这样的模式就可以完成任务了:

csvPromise
.then(function(arr) {
    output(arr); // observe initial array as delivered by csvPromise
    return methodA(arr);
})
.then(function(arr) {
    output(arr); // observe array after application of methodA
    return methodB(arr);
})
.then(function(arr) {
    output(arr); // observe array after application of methodB 
    return methodC(arr);
})
etc..
etc..
etc..
.then(function(arr) {
    output(arr); // observe array after application of final method
}).catch(function(error) {
    console.log(error); // any error thrown from anywhere in the chain will arrive here 
});
模式可以通过动态构建链来实现程序化,如下所示:

var methods = [methodA, methodB, methodC, ...]; // list of methods (uncalled at this point)
return methods.reduce(function(promise, method) {
    return promise.then(function(arr) {
        output(arr); // observe result of previous stage
        return method(arr);
    });
}, csvPromise)
.then(function(arr) {
    output(arr); // observe array after application of final method
}).catch(function(error) {
    console.log(error); // any error thrown from anywhere in the chain will arrive here 
});
最有可能违反这些假设的是
output()
本身是异步的,在这种情况下:

var methods = [methodA, methodB, methodC, ...]; // list of methods
return methods.reduce(function(promise, method) {
    return promise.then(function(arr) {
        return output(arr).then(function() {
            return method(arr);
        });
    });
}, csvPromise)
.then(function(arr) {
    output(arr); // observe array after application of final method
}).catch(function(error) {
    console.log(error); // any error thrown from anywhere in the chain will arrive here 
});

那么更改是累积的,还是每个更改都基于相同的原始数组?累积的,每个更改都是前一个更改的加法,并且每个更改在处理之前由U.I.中的选择决定(例如,标准化与标准化)。