I try to load a text file with d3.text api as below, but it return a promise. I would expect it return the text file content.
async function loadSync(filename) {
return await d3.text(filename)
}
function load_address() {
var url = "https://example-files.online-convert.com/document/txt/example.txt"
var addrs = loadSync(url)
console.log(addrs)
}
load_address()
<script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>
2
Answers
Probably you need to fetch the file first and then do the other things
There are a couple of things going on here.
First, you’re right that the result is a promise. Be sure to
await
within anasync function
at the appropriate time.Second, you’ll need to work around the Cross-origin policy issue with a CORS Proxy.
Of course, this is pretty easy to do with
fetch
as well: