1

A simple script, what I am trying to achieve is pushing new item in the append method, and using asynchronous functions

I'm trying to understand how then and catch works instead of using them without understanding how they work inside ( using axios or something )

The push has to be executed after 3 seconds

I tried to use the resolve method inside setTimeout but I am getting an error, because resolve isn't recognized, I am returning a Promise because I can't await a setTimeout

<script>

    async function test(terms) {
        let termss = await append(terms);
        return [termss[termss.length - 2], termss[termss.length - 1]]
    }

    async function append(arr) {
        var arrr = arr;

        const waitFor = () => new Promise(resolve => {
            setTimeout((resolve) => {
                arrr.push("Taoufiq")
                arrr.push("understands");
            }, 3000)
        });

        await waitFor();

        return arrr;
    }

    test([1, 2, 3, 9]).then((result) => {
        console.log(result)
    })

</script>

Ayy help on this to understand how this works ?

My expected result is returning an array with ["Taoufiq", "understands"]

1
  • 1
    You do, indeed, need to resolve that promise. Look at this example to see it in action. There is no need to pass resolve to the timeout function. it is in scope there. Commented Dec 18, 2021 at 15:02

1 Answer 1

1

You should be able to do what you want with the following code:

async function test(terms) {
    let termss = await append(terms);
    return [termss[termss.length - 2], termss[termss.length - 1]]
}

async function append(arr) {
    return new Promise(resolve => {
        setTimeout(() => {
            arr.push("Taoufiq")
            arr.push("understands");
            resolve(arr)
        }, 3000)
    });
}

test([1, 2, 3, 9]).then((result) => {
    console.log(result)
})

So you have to return the new Promise inside append function and use the resolve method that Promise constructor gives to you inside the setTimemout.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks fro writing this, this is correct but also you wrote something that helped me undertands, so the resolve(param) or reject(param) is ways to return value, so await a Promise return the value in the res or rej method, isn't it ? like a replacement for return
@TaouBen yes, you are right. You can return a value from inside a Promise using resolve and reject methods and you can "await" that values outside the promise with then, catch or await. Pay attention that if you do something like const res = await promise(), if the promise throw an error you have to wrap that statement inside a try..catch block in order to get the error. Any way you find all in the doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.