0

I have used a for loop but all the functions get triggered at once, the array.push is not working obviously and I get empty as results

 const results = []
    for (let x in obj) {
        const url = 'https://api.test.com/v2/text?image_url=' + encodeURIComponent(obj[x]);
        (async () => {
            try {
                const response = await got(url, {
                    username: apiKey,
                    password: apiSecret,
                });
                results.push({ response: response.body, url: obj[x] })
            } catch (error) {
                console.log(error);
            }
        })();
    }
    console.log('results :', results);
Youssef
  • 565
  • 1
  • 5
  • 21
  • 2
    You are printing the result before the async function could add items. Use `await` on the async function. – some-user Sep 18 '22 at 14:45

0 Answers0