I am trying to fetch a textfile from my server. The textFile contains image paths with which I want to make a gallery.
This code works and sows the image callery in console:
function loadGallery() {
const url = "/products/nfts/filelist.txt"
fetch(url)
.then( response => response.text())
.then( text => text.replace("\r", ""))
.then( cleaned => cleaned.split("\r"))
.then( array => console.log(array));
}
this code doesnt, it tells me the variable imagelist
is undefined:
let imagelist;
function loadGallery() {
const url = "/products/nfts/filelist.txt"
fetch(url)
.then( response => response.text())
.then( text => text.replace("\r", ""))
.then( cleaned => cleaned.split("\r"))
.then( array => (imagelist=array));
console.log(imagelist);
}
please help me on how to assign this variable.
Update: I found out that console.log
will find an assigned imagelist
once i trigger it from a button manually later on:
function getImages(){
console.log(imagelist);
}
so I think its loading asynchronous. How do I wait for the fetch completion?
the following code is not working either:
let imagelist;
function loadGallery() {
const url = "/products/nfts/filelist.txt"
fetch(url)
.then( response => response.text())
.then( text => text.replace("\r", ""))
.then( cleaned => cleaned.split("\r"))
.then( array => (imagelist=array))
.then(getImages());
}
function getImages(){
console.log(imagelist);
}