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

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...