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"]

VLAZ
  • 26,331
  • 9
  • 49
  • 67
TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • 1
    You do, indeed, need to resolve that promise. Look [at this example](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#examples) to see it in action. There is no need to pass `resolve` to the timeout function. it is in scope there. – Randy Casburn Dec 18 '21 at 15:02

1 Answers1

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.

Marco Nisi
  • 1,211
  • 10
  • 13
  • 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 Dec 18 '21 at 15:18
  • 1
    @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. – Marco Nisi Dec 18 '21 at 16:09