How to set background image of body after image is loaded?

You could use Image() to load it, listen for onload and then add it to the dom when it is loaded.

let img = new Image();

img.onload = function(){
    document.body.style.backgroundImage = "url('" + img.src + "')";
};

img.src = 'imageUrl.jpg';