Skip to content

Promise D3 js inside ‘then’

Trying to understand the different steps to the Promise implementation for D3 js.

Have two input files and promise them all:

Promise.all([jsonFile, txtFile]).then(input)

The array has been consolidated into one individual input, which might be called as a one-parameter function like:

function input(data) {
 console.log(data[0])  // json
 console.log(data[1])  // txt
}

Imagine I want to implement the second function as a two-parameter with the two inputs like function input(json, txt). What should happen in the then() statement to make the code do so?

Thanks

Answer

If you want to implement input function with two parameters:

function input(json, txt) {
  // omitted
}

then you can use rest parameters [more]:

Promise.all([jsonFile, txtFile])
  .then((data) => input(...data))

or you can be more explicit:

Promise.all([jsonFile, txtFile])
  .then(([json, txt]) => input(json, txt))