I'm making an "Image" constructor function. It saves metadata from arguments, and fetches the image (based on metadata).
function Image(filename) {
this.filename = filename;
fetch(`https://example.com/${this.filename}`)
.then( response => response.blob())
.then( blob => {
this.objectURL = URL.createObjectURL(blob);
this.timestamp = Date.now();
})
}
However, when ran with new Image('image.jpg')
, it returns an object with only filename property, and objectURL with timestamp are appended later.
What I want is constructor function to wait for objectURL and timestamp before returning an object.
Thanks in advance!
Additionally, you can see demonstration of running it in console, where title and number are arguments, filename is computed from them and used in real link, and dataURL, timestamp, pinned properties are set inside then().