'NodeJS: promiseAll getting stuck

One of the promises is not being resolved, what can be the issue possibly?

const items = await Promise.all(data.map(async i => {
        const tokenUri = await contract.tokenURI(i).catch(function (error) {return;});
        if (tokenUri.length < 8) {
            return;
        }
        let url = "http://127.0.0.1:5001/api/v0/get?arg=" + tokenUri.slice(7)
        const meta = await axios.post(url).catch(function (error) {return;});
        success++;
    }), function (err) {
        callback(err);
    })


Solution 1:[1]

This part of the code is misplaced:

, function (err) {
    callback(err);
})

Currently, it is being passed as a second argument to Promise.all(). Promise.all() only takes one argument (an iterable like an array) so that's being ignored and that callback will NEVER get called.

If you meant for that to be like the second argument to .then(), then you'd have to have a .then() to use it with, but you're trying to use it as second argument to Promise.all() and that is not correct.

If you want to know when Promise.all() is done, you would do this:

try {
    const items = await Promise.all(data.map(async i => {
        const tokenUri = await contract.tokenURI(i).catch(function(error) { return; });
        if (tokenUri.length < 8) {
            return;
        }
        let url = "http://127.0.0.1:5001/api/v0/get?arg=" + tokenUri.slice(7)
        const meta = await axios.post(url).catch(function(error) { return; });
        success++;
    }));
    console.log("Promise.all() is done", items);
} catch (e) {
    console.log(e);
}

Note, you are silently "eating" errors with no logging of the error inside this loop which is pretty much never a good idea.


If, as you propose, one of your promises is actually never being resolved/rejected, then none of this code will do anything to fix that and you will have to go down to the core operation and find out why it's not resolving/rejecting on its own. Or, configure a timeout for it (either in the API or by manually creating your own timeout).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1