About

Friday, 12 November 2021

Promises in Javascript(JS)

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

    setTimeout(()=> {
        let roll_no = [1,2,3,4,6];
        resolve(roll_no);
        // reject("Error while communicating!");
    },2000);
});

const getBiodata = (datas)=>{
    return new Promise((resolve, reject)=> {
        setTimeout(()=> {
            let biodata = {
                name:'vinod',
                age: 23
            }
            resolve(`My roll number is ${datas}. My name is ${biodata.name}. While I
am ${biodata.age} years old.`)
        },2000);
    });
}

// pobj.then((rollno)=> {
//     console.log(rollno);
//     getBiodata(rollno[1]).then((dam)=>{
//         console.log(dam);
//     })
// }).catch((error)=>{
//     console.log(error);
// })

async function getData(){
    const rollnodata = await pobj;
    console.log(rollnodata);

    const biodatas = await getBiodata(rollnodata[1]);
    console.log(biodatas);
}

No comments:

Post a Comment