问题
I have already read these answers, but I wasn't able to solve:
Adapt old geo to D3 v5, how to express a queue of Promise?
d3.js v5 - Promise.all replaced d3.queue
My code in d3 v4 looks like
d3.queue()
.defer(d3.json, "path/file.json")
.defer(populate,map,data)
.await(ready);
}
function populate(map,data,callback) {
.. code ..
callback(null);
}
function ready(error, topo) {
.. code ..
}
I would like to replace this with Promise
回答1:
I'm assuming your populate function returns topo. You can use promise chaining:
function populate(map, data) {
.. code ..
return // return topo?
}
function ready(topo) {
// map
.. code ..
}
Promise.all([d3.json("path/file.json")])
.then(([data]) => populate(map, data))
.then(topo => {
ready(topo)
})
Instead of doing continuation using a callback, you can return the value normally, and it'll be picked up by the next function in the chain.
来源:https://stackoverflow.com/questions/62952888/how-to-express-a-queue-of-promise-in-d3-v5