score:1
Accepted answer
Assuming :
- you have a starting promise named
csvPromise
, which delivers array - the methods to be applied are
methodA
,methodB
,methodC
etc, each of which accepts Array - each method either returns a mutation of the input array or delivers the mutation via a Promise
- changes to the array are cumulative, method-on-method
- you have a synchronous function
output()
which will accept the original array and each mutation
then a pattern like this will do the job :
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
});
The pattern can be proceduralised by building the chain dynamically as follows :
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
});
The most likely violation of the assumptions would be that output()
was itself asynchronous, in which case :
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
});
Source: stackoverflow.com
Related Query
- Outputting Value of Promise during multiple parses
- How to set multiple attributes with one value function?
- In D3, how can I create multiple elements for each data element based on value in data?
- Get max value from array with multiple columns
- Draw multiple line chart using d3.entries key value pair
- How to add multiple images to svg in D3 (quantity based on value in dataset)
- d3 Grouping data on multiple values in combination with key value pair
- D3 in react is creating multiple graphs of value update
- d3 multiple line chart tooltip value data undefined
- Update size and value of multiple donut charts d3.js
- Is it expensive to call D3.js transition multiple times during scroll?
- Error: Invalid value for <path> attribute -- During a d3 Projection Transition -- Infinity & NaN
- D3.js how to extract Y domain value from data array with multiple Y axis columns
- How to return a value from a promise which isn't a pending promise: d3.js, Javascript?
- Drawing multiple horizontal lines with the same value on ordinal scale in d3.js
- Appending multiple non-nested elements for each data member with D3.js
- d3 add multiple classes with function
- Drawing Multiple Lines in D3.js
- How to select multiple selectors with selectAll?
- How to use D3 selectAll with multiple class names
- d3.select by attribute value
- Drawing multiple edges between two nodes with d3
- Importing data from multiple csv files in D3
- D3.js: get value of selected option?
- d3 color scale - linear with multiple colors?
- Multiple partners in a family tree in d3.js?
- D3.js How to apply multiple classes when using a function
- Determine if Shift key is pressed during mousedown event
- D3: update data with multiple elements in a group
- d3 retrieve and add to current selection's attribute value
More Query from same tag
- How to change text color of legend in d3js graph?
- Adding links to a D3 map
- How do you push a new node onto an existing node in D3 in a tree layout?
- Js object key: plus value syntax
- Appending svg into a background-image
- Please advice D3.js for CSV data import
- d3.js tool tip on a force directed graph not displaying complete data while hovering over links
- Chaining Transforms in D3
- Is there anyway to export D3 JS graphs into LaTeX tikz graphs programmatically?
- make a title for <image> of SVG
- Increase link length in node-link graph using d3
- Load and modify svg files from node.js
- SVG Wrapping text on x-axis
- How do I override d3.js chart function behavior?
- How to make this D3 horizontal bar chart work?
- How to apply a CSS style sheet to a D3 force-directed graph?
- Different shape/image for leaf nodes in pack layout
- Updating NVD3 /D3 chart as per user input radio button
- Dynamically Update Multiple Charts in D3
- How to stop paths from blurring/disappearing in D3 when using a viewbox?
- d3.js - how to adjust the height of the brush selection
- d3.js transition() not working when browser is minimised
- d3 grouped bar chart inconsistent items alignment
- using dynamic list data in d3.js
- Why are my SVG foreign object text not showing up?
- Handling nested data arrays
- D3 V4 zoom.transform jump on drag
- D3.js tipsy tooltips
- Calculate bounding polygon of alpha shape from the Delaunay triangulation
- Download C3 chart as pdf as given download option in D3 graphs