Is it possible to invoke a function from another function?

I’d like to implement Mongo connection pooling as seen here:

Is it possible to await a response from another Netlify function?


I think you can, but I’m not sure why would you choose this way. Why not combine it in one function?

An alternative way that I choose is, I make multiple requests from client side. For example, I fetch my first function in my client-side JavaScript, and once it returns a response, I fetch the second function, something like:

fetch('/.netlify/functions/foo/', {method: 'POST'}).then(response => {
  if (response.ok) {
    return response // or response.json()
  } else {
    throw response.statusText
  }).then(data => {
    fetch('/.netlify/functions/bar/', {method: 'POST'}.then(same as before))
  }).catch(error => console.log(error))

Thanks for the reply.

The purpose is to have a function responsible for connecting to the database.
This function will be invoked by all other functions and stay warm, retaining the open connection.

Connecting to mongodb again for every invocation from client side slows the system down significantly.

The open connection would be terminated in 10 seconds and those 10 seconds are counted from the moment the function was invoked. So, I’m not sure if that works for you but yes, you should be able to use a Function within a Function.

This is more about the ‘warm’ container that persists in lambda. So long as you keep using a lambda function then it will remain provisioned in memory and retain the open connection the DB

Here is a simpler explanation from mongo themselves:

So how do I trigger a function from another function?
Run netlify cli and call invoke-function?
Make a fetch request? If so to what endpoint?


I get your point, but I’m not 100% sure if Netlify Functions keep those containers or terminate them after the timeout. You’ll have to try it out or wait for someone else to reply.

About how to call the function, it’s fairly easy. Once you run netlify dev, your functions are accessible according to their names. So, a function named ‘hello.js’ would be available at /. netlify/functions/hello/. Then, you’ll probably need a module like Node Fetch which would help you fetch the other function from within the function.

I’ve tested it out and the containers do stay alive since between invocations.

I’ll try node fetch out and see whether it can reach other functions at /.netlify/functions./..

Hey there, @Charles.Sapien! Let us know what you find with that attempt. :slight_smile:


For the time being I am going with the more simple approach of just keeping a mongodb client singleton in every function.
The risk is that I will use too many connections to mongo and get left with “zombie” connections when the function container is disposed.

Right now havent seen any problem in the DB connection monitor so may be fine as is.

1 Like

Sounds great, @Charles.Sapien . Let us know if other questions related to this come up :slight_smile: