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 i can't get products table using promise function in javascript?

I followed a javascript tutorial, in this video it applies the promise function, in my case I am waiting to retrieve it from the products table but it gives me undefined.

getUser(10)
 .then((user) => {
    console.log(user);
    getProducts(user.id);
   })
 .then(products => console.log(products))
 .catch((err) => console.warn(err))
 

function getUser(id){
    return new Promise((resolve,reject) => {
        let status = true;

        setTimeout(() => {
            console.log('return data');
            if(status){
                return resolve({ id:id, name: 'najib' })
            }else{
                return reject("user not found")
            }
        }, 2000)
    })
}

function getProducts(userId){
    return new Promise((resolve,reject) => {
        let status = true;

        setTimeout(() => {
            console.log('return product data');
            if(status){
                return resolve(['p1','p2','p3','p4'])
            }else{
                return reject("products not found")
            }
        }, 2000)
    })
}

>Solution :

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

you miss a return after console.log(user)

getUser(10)
 .then((user) => {
    console.log(user);
    return getProducts(user.id);
   })
 .then(products => console.log(products))
 .catch((err) => console.warn(err))
 

function getUser(id){
    return new Promise((resolve,reject) => {
        let status = true;

        setTimeout(() => {
            console.log('return data');
            if(status){
                return resolve({ id:id, name: 'najib' })
            }else{
                return reject("user not found")
            }
        }, 2000)
    })
}

function getProducts(userId){
    return new Promise((resolve,reject) => {
        let status = true;

        setTimeout(() => {
            console.log('return product data');
            if(status){
                return resolve(['p1','p2','p3','p4'])
            }else{
                return reject("products not found")
            }
        }, 2000)
    })
}
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