javascripttypescriptpromiseangular-promise

How to call Promise inside Promise and call function 2 times at a time in javascript


My code scenario is like :-

async function ot(a: any) {
    return "outer text " + a;
}

async function it() {
    return "inner text";
}

function insert() {
    it().then(res => {
        console.log(res);
        ot(res).then(resp => {
            console.log(resp);
        });
    });
}

insert();
insert();

NOTE:- I have called insert() function 2 times

Code output:-

"inner text" 
"inner text" 
"outer text inner text"
"outer text inner text"

expected output:-

"inner text"
"outer text inner text"
"inner text"
"outer text inner text"

I want to call insert function more then one time at a single time, is there any way to reach it?

Thank you very much in advance


Solution

  • Use then

    async function ot(a) {
        return "outer text " + a;
    }
    
    async function it() {
        return "inner text";
    }
    
    function insert() {
        return it().then(res => {
            console.log(res);
            return ot(res).then(resp => {
                console.log(resp);
            });
        });
    }
    
    insert().then(() => insert());