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 :

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)
    })
}

Leave a Reply