Sunday, January 2, 2022

A promise can return another promise

 Let's see the below example,

const ids = new Promise((resolve, reject) => {

    setTimeout(() => {

        resolve([100, 200, 300, 400, 500]);

    }, 1500);

});

const getReceipe = recId => {

    return new Promise((resolve, reject) => {

        setTimeout(id => {

            const receipe = {

                title: 'potato',

                publisher: 'jonas'

            };

            resolve(`${id}:${receipe.title}`);

        }, 1500, recId);

    });

};

ids.then(ids => {

        console.log(ids);

        return getReceipe(ids[2]);//returning another promise object

    })

    .then(receipe => {

        console.log(receipe);

    })

    .catch(error => {

        console.log(error);

    })

No comments:

Post a Comment

Element of a good table (Ref: Database design mere mortals by Michael J. Hernandez)

  Elements of the Ideal Table: It represents a single subject, which can be an object or event that reduces the risk of potential data integ...