Back

[SOLVED] Promise.all not responding res.json(), console.log not working

  • 0
  • Self Hosted
elemin67
11 May, 2023, 22:35

i dont get any response. There is also not log inputs. The server goes to 500 error

How I can use the res.json and console.log inside Promise.all?

TL;DR
The user was having trouble with `res.json()` not responding and `console.log()` not working inside `Promise.all()`. The solution is to add the `await` keyword before `Promise.all()`. Wrapping code in backticks for formatting is recommended.
Drake
11 May, 2023, 23:07

is this an Appwrite Function? Is this all the code?

elemin67
11 May, 2023, 23:08

Function

elemin67
11 May, 2023, 23:09

I create now a fresh function with follwoing code: module.exports = async function (req, res) { const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 'foo'); });

Promise.all([promise1, promise2, promise3]).then((values) => { console.log('test') res.json({values}); }); };

elemin67
11 May, 2023, 23:10

res.json is working, but console.log not

Drake
11 May, 2023, 23:10

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

Drake
11 May, 2023, 23:11

can you try adding await before Promise.all()?

elemin67
11 May, 2023, 23:11
TypeScript
module.exports = async function (req, res) {
  const promise1 = Promise.resolve(3);
  const promise2 = 42;
  const promise3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 100, 'foo');
  });
  
  Promise.all([promise1, promise2, promise3]).then((values) => {
    console.log('test')
    res.json({values});
  });
};
elemin67
11 May, 2023, 23:11

testing

elemin67
11 May, 2023, 23:13
TypeScript
module.exports = async function (req, res) {
  const promise1 = Promise.resolve(3);
  const promise2 = 42;
  const promise3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 100, 'foo');
  });
  
  await Promise.all([promise1, promise2, promise3]).then((values) => {
    console.log('test')
    res.json({values});
  });
};

that helps. Thanks!

Drake
11 May, 2023, 23:33

[SOLVED] Promise.all not responding res.json(), console.log not working

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more