# Chaining of edge func gives 502

**URL:** <https://answers.netlify.com/t/chaining-of-edge-func-gives-502/71285>\
**Category:** Support\
**Tags:** edge\_functions\
**Created:** [July 13, 2022, 8:45pm UTC](https://answers.netlify.com/t/chaining-of-edge-func-gives-502/71285 "2022-07-13T20:45:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danv](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/danv/32/39297_2.png) [@danv](https://answers.netlify.com/u/danv)\
**Post date:** [July 13, 2022, 8:45pm UTC](https://answers.netlify.com/t/chaining-of-edge-func-gives-502/71285/1 "2022-07-13T20:45:56Z")

</div>

I try to implement chaining pattern when one function is calling another, and that another one in this way

```auto
await fetch(req.url.replace('day', 'week'))

```

[https://ME.netlify.app/getday?day=2022-07-10](https://ME.netlify.app/getday?day=2022-07-10) to [https://ME.netlify.app/getweek?day=2022-07-10](https://ME.netlify.app/getweek?day=2022-07-10)

it works fine on local “netlify dev” but after deploy its generate 502 error:

```auto
 Response {
  body: ReadableStream { locked: true },
  bodyUsed: true,
  headers: Headers {
  "content-length": "62",
  "content-type": "text/plain; charset=utf-8",
  date: "Wed, 13 Jul 2022 20:36:32 GMT",
  server: "Netlify",
  "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
  "x-nf-request-id": "01G7WME3PH1VXFKZEPFHGR6QXB"
},
  ok: false,
  redirected: false,
  status: 502,
  statusText: "Bad Gateway",
  url: "https://ME.netlify.app/getweek?day=2022-07-10"
}

```

how to fix this?

---

<div class="post-metadata">

**Author:** ![skn0tt](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/skn0tt/32/33874_2.png) [@skn0tt](https://answers.netlify.com/u/skn0tt)\
**Post date:** [July 14, 2022, 8:17am UTC](https://answers.netlify.com/t/chaining-of-edge-func-gives-502/71285/2 "2022-07-14T08:17:56Z")

</div>

Hi Dan, and welcome to the Forums!

Using `fetch` for calling another edge function results in an endless loop, because the same edge function that’s making the `fetch` request will be invoked again as part of the downstream request.

If you want to chain edge functions together, I recommend you take a look at `context.next` (see [Edge Functions API | Netlify Docs](https://docs.netlify.com/netlify-labs/experimental-features/edge-functions/api)).  
If your goal is to share code between functions, it might be easier to share them on a code level, and import shared logic in all edge functions where it’s used.

Let me know if that helped!

Best Regards  
Simon
