Skip to main content
Post Closed as "Duplicate" by Felix Kling, CommunityBot
added 28 characters in body
Source Link
Michalis
  • 7k
  • 14
  • 56
  • 86

I have this simple example to my controller and doesn't work as expected

export let create = async (req: Request, res: Response) => { console.log("START"); await setTimeout(() => { console.log("MIDDLE"); }, 1000); console.log("END"); return res.json({ data: null }); }; 

Output: START,END,MIDDLE

EXPECT: START,MIDDLE,END

I have this simple example to my controller and doesn't work as expected

export let create = async (req: Request, res: Response) => { console.log("START"); await setTimeout(() => { console.log("MIDDLE"); }, 1000); console.log("END"); return res.json({ data: null }); }; 

Output: START,END,MIDDLE

I have this simple example to my controller and doesn't work as expected

export let create = async (req: Request, res: Response) => { console.log("START"); await setTimeout(() => { console.log("MIDDLE"); }, 1000); console.log("END"); return res.json({ data: null }); }; 

Output: START,END,MIDDLE

EXPECT: START,MIDDLE,END

edited tags
Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k
Source Link
Michalis
  • 7k
  • 14
  • 56
  • 86

NodeJs - Async/Await inside controller

I have this simple example to my controller and doesn't work as expected

export let create = async (req: Request, res: Response) => { console.log("START"); await setTimeout(() => { console.log("MIDDLE"); }, 1000); console.log("END"); return res.json({ data: null }); }; 

Output: START,END,MIDDLE