Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a REST API With Express Building API Routes in Express Using Middleware in Express

Thomas Dimnet
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Dimnet
Python Development Techdegree Graduate 43,623 Points

Manage multiple asynchronous calls with Express

Hi there,

I understand the id of the next middleware and when to use it. But I am wondering how to work with multiple asynchronous calls with express (either when building an express rest api or when rendering templates directly with express and pug).

Do we use promises and new ES7 methods like async/await or is there some other solutions?

Thanks!

Thomas.

1 Answer

d4rk5un
d4rk5un
2,771 Points

Hi Thomas

I believe you can use both promises and callback functions to do async calls.

But using promises is a lot cleaner and can prevent calling functions more than once, since the promise will return and will not execute the rest.

I suggest you have a look at axios, it is fantastic package.

https://www.npmjs.com/package/axios

I hope this helps.

Good luck

K.