How do I return the variable assigned by the asynchronous method within the function?

 const list = [
        "./img.png",//
        "//be-fe.github.io/static/images/iSlider-card/10.jpg",//
        "./load.gif",//
        "//be-fe.github.io/static/images/iSlider-card/12.jpg",//
        null,//
        "//be-fe.github.io/static/images/iSlider-card/17.jpg",//
        ];    
        const placeholder = "";
        const loadError = "";
        const gen = () => {
            let endSrc = placeholder;
            let html = "";
            list.forEach((ele) => {
                if(ele){
                    const image = new Image();
                    image.src = ele;
                    image.onload = () => {
                        endSrc = ele;
                    };
                    image.onerror = () => {
                        endSrc = loadError;
                    };
                }
               html += `

${endSrc}

`; }); // html onload onerror[loaderror ] return html; } $("body").append(gen())


use Promise

const gen = () => {
  return Promise.all(list.map(ele => {
    if (ele) {
      return new Promise(resolve => {
        const image = new Image();
        image.src = ele;
        image.onload = () => {
          resolve(`

${ele}

`) }; image.onerror = () => { resolve(`

${loadError}

`) }; }) } else { return Promise.resolve(`

${placeholder}

`) } })).then(arr => arr.join('')); } gen().then(html => { $("body").append(html) })

const list = [
    "./img.png",//
    "//be-fe.github.io/static/images/iSlider-card/10.jpg",//
    "./load.gif",//
    "//be-fe.github.io/static/images/iSlider-card/12.jpg",//
    null,//
    "//be-fe.github.io/static/images/iSlider-card/17.jpg",//
];
const placeholder = '';
const loadError = '';
const gen = () =>
    list.map((ele) => new Promise(resolve => {
        function getsrc(src) {
            resolve(`

${src}

`) } if(ele){ const image = new Image(); image.src = ele; image.onload = () =>getsrc(ele) image.onerror = () => getsrc(loadError) }else { getsrc(placeholder) } })) Promise.all(gen()).then(htmlArr => $("body").append(htmlArr.join('')));

< H2 > callback method < / H2 >
const gen = () => {
    return new Promise((res, rej) => {
        let endSrc = placeholder;
        let html = '';
        list.forEach(ele) {
            if (ele) {
                const image = new Image();
                image.src = ele;
                image.onload = () => {
                    endSrc = ele;
                };
                image.onerror = () => {
                    endSrc = loadError;
                };
            }
            html += `

${endSrc}

`; }); res(html); } } gen() .then(html => { $("body").append(html); })

and asnyc await , etc.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35ef2-e522.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35ef2-e522.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?