About

Saturday 13 November 2021

Async Await in Javascript(JS)

 Technique 1 for the async await

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

    setTimeout(() => {
        let roll_no = [1, 2, 3, 4, 5];
        resolve(roll_no);
    }, 2000);
});

const getBiodata = (indexdata) => {
    return new Promise((resolve, reject) => {
        setTimeout((indexdata) => {
            let biodata = {
                name : 'Vinod',
                age : 26
            }
            resolve(`My name is ${biodata.name} and I am ${biodata.age} years old`);
        }, 2000, indexdata);
    });
}
async function getData(){
    const rollnodata = await pobj1;
    console.log(rollnodata);

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

}
getData();

//Technique 2 for async-await

/*
const pobj1 = new Promise((resolve, reject) => {

    setTimeout(() => {
        let roll_no = [1, 2, 3, 4, 5];
        resolve(roll_no);
    }, 2000);
});

const getBiodata = (indexdata) => {
    return new Promise((resolve, reject) => {
        setTimeout((indexdata) => {
            let biodata = {
                name : 'Vinod',
                age : 26
            }
            resolve(`My name is ${biodata.name} and I am ${biodata.age} years old`);
        }, 2000, indexdata);
    });
}
async function getData(){
    const rollnodata = await pobj1;
    console.log(rollnodata);

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

}
getData();
*/
// New Technique for async await

/*
console.log('person1: shows ticket');
console.log('person2: shows ticket');

const promiseWifeBringTick = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('nunu');
    }, 2000);
})
const getPopcorn = promiseWifeBringTick.then((t) => {
    console.log('husband: we should get in');
    console.log('wife: no I am hungry');
    return new Promise((resolve, reject) => resolve(`${t} popcorn`));
    // console.log(`person3: shows ${t}`);
});

var getButter = getPopcorn.then((t) => {
    console.log('husband: we should go now');
    console.log('wife: I need butter in my popcorn');
    return new Promise((resolve, reject) => resolve(`${t} butter`));
});
getButter.then((t) => console.log(t));

console.log('person4: shows ticket');
console.log('person5: shows ticket');
*/
/*
console.log('This is the new technique');

async function bhaba(){
    console.log("Inside Bhaba function")
    const response = await fetch('https://api.github.com/users');
    console.log('Before Response');
    const users = await response.json();
    return users;
}
console.log("Before calling Bhaba");
let a = bhaba();
console.log('After calling Bhaba');
console.log(a);
a.then(data => console.log(data) )
console.log("This is the last line of the code");
*/

console.log('person1: show ticket');
console.log('person2: show ticket');

const preMovie = async() => {
    const promiseWifeBringTick = new Promise((resolve, reject) => {
        setTimeout(() => resolve('Wife got the Ticket'), 3000);
    });

const getPopcorn = new Promise((resolve, reject) => resolve(`actoo popcorn`));
const getButter = new Promise((resolve, reject) => resolve(`sitajakhala butter`));
    let ticket = await promiseWifeBringTick;

    let popcorn = await getPopcorn;
    console.log(`Husband: I got some ${popcorn}`);
    let butter = await getButter;
    console.log(`Husband: I got some ${butter} as well`);

    return ticket;
}
preMovie().then((m) => console.log(`${m}`));.

No comments:

Post a Comment