Context in the sunrise - sunset pack


I have another question related to the sunrise - sunset pack.

I understand that context refers to an object to which a method (or function in an object) belongs.

With that in mind, what does context mean in this line of code?

execute: async function ([lat, lng, date], context) {

Hi @Lynn_vK - Good question! The context variable holds an object of the type ExecutionContext. You can basically think about it as a toolbox that Coda hands your formula, containing various settings and tools. For example, the Fetcher which is used to make HTTP requests is held inside the context, at context.fetcher.

To break down that line:

  • execute: - A key in the formula definition object, saying that the thing after it is the function to call when executing the formula.
  • async function() - The declaration of a JavaScript function, which runs asynchronously. This means that the function can use await to wait for another asynchronous function to finish (usually the fetcher in our case).
  • [lat, lng, date] - The first parameter to the function, destructured into individual variables. All of the parameters passed into the formula are sent to your execute function as a single array parameter. This like breaks them back out of that array.
  • context - The second parameter to your execute function, which is the context variable.

Let me know if that helps clear it up at all.

1 Like

Thank you Eric. That helps.