Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my promise return undefined even with async/await?

Promise returns undefined

const encontrarMateria = (mat)=>{
    let mater = materias[0]
    const prom = new Promise((res,rej)=>{
        mater = materias[mat];
        alert(materias[mat].materia);
        if (mater=undefined){
            return rej("no se ha encontrado materia");
        }else{
            return res(mater);
        }
    })
    console.log(prom)
    return prom();
}

even if i put async

const encontrarMateria = async(mat)=>{
    let mater = materias[0]
    const prom = await new Promise((res,rej)=>{
        mater = materias[mat];
        alert(materias[mat].materia);
        if (mater=undefined){
            return rej("no se ha encontrado materia");
        }else{
            return res(mater);
        }
    })
    console.log(prom)
    return prom();
}

but if i change materia[mat] instead of mater it returns all ok, why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const encontrarMateria = async(mat)=>{
    let mater = materias[0]
    const prom = await new Promise((res,rej)=>{
        mater = materias[mat];
        alert(materias[mat].materia);
        if (mater=undefined){
            return rej("no se ha encontrado materia");
        }else{
            return res(materia[mat]);
        }
    })
    console.log(prom)
    return prom();
}

example of materias array

const materias = [{
    materia:"fisica 1",
    nota:10
},{
    materia:"matematica 1",
    nota:9
},{
    materia:"algebra 1",
    nota:9
},{
    materia:"quimica 1",
    nota:8
}];

i try to get an object mater=materias[x] but I get undefined, Only when i put the object and not the variable instead i can reach the value, why?

>Solution :

Your code doesn’t make a lot of sense because it’s sync anyway, also learn how to compare variables in JS, also there’s no sense in wrapping your promise in an extra function. The code could look like that, but again it should be done without a promise, this is an example how it could be done in async if there’s a need in async (for example some backend api call), when you use a promise instance here you don’t need to use async/await also:

const encontrarMateria = mat => new Promise((res, rej) => {
        const mater = materias[mat];
        if (mater === undefined){
            return rej(new Error("no se ha encontrado materia"));
        }else{
            return res(mater);
        }
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading