# When the asynchronous function is invoked, wrap the callback in a closure that will restore context before executing the original re entry code

**URL:** <https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316>\
**Category:** APM\
**Created:** [August 14, 2018, 9:59am UTC](https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316 "2018-08-14T09:59:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d3v0ps](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@d3v0ps](https://discuss.elastic.co/u/d3v0ps)\
**Post date:** [August 14, 2018, 9:59am UTC](https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316/1 "2018-08-14T09:59:35Z")

</div>

I just read the NewRelic documentation about the Context Preservation and they have this statement which I don't understand:

> When the asynchronous function is invoked, wrap the callback in a closure that will restore context before executing the original re entry code.

ref: [https://github.com/newrelic/node-newrelic/blob/master/examples/shim/Context-Preservation.md](https://github.com/newrelic/node-newrelic/blob/master/examples/shim/Context-Preservation.md)

Is anybody here can explain in details this statement ? Maybe a sample code would be appreciated. Thanks you so much.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [August 14, 2018, 12:04pm UTC](https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316/2 "2018-08-14T12:04:26Z")

</div>

Hi and welcome to the Elastic APM forum.

I think you have posted your question in the wrong forum, however. Elastic is not affiliated with NewRelic.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [August 14, 2018, 2:19pm UTC](https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316/3 "2018-08-14T14:19:41Z")

</div>

Hi @d3v0ps

As my colleague Felix mentioned, then this is probably not the right forum to ask about New Relic 😉

That being said, I'm one of the developers of the [Elastic APM Node.js Agent](https://www.npmjs.com/package/elastic-apm-node), and I'll be happy to answer any questions you might have related to that if you should ever want to try it out 🙂

Read more about Elastic APM here: [https://www.elastic.co/solutions/apm](https://www.elastic.co/solutions/apm)

Best,  
Thomas

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 4, 2018, 10:19am UTC](https://discuss.elastic.co/t/when-the-asynchronous-function-is-invoked-wrap-the-callback-in-a-closure-that-will-restore-context-before-executing-the-original-re-entry-code/144316/4 "2018-09-04T10:19:44Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
