# Struggling with async function

**URL:** https://discourse.nodered.org/t/struggling-with-async-function/38574
**Category:** General
**Created:** [5 January 2021 09:40 UTC](https://discourse.nodered.org/t/struggling-with-async-function/38574 "2021-01-05T09:40:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![msupra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/msupra/32/34760_2.png) [@msupra](https://discourse.nodered.org/u/msupra)
#### Post date: [5 January 2021 09:40 UTC](https://discourse.nodered.org/t/struggling-with-async-function/38574/1 "2021-01-05T09:40:16Z")

</div>

Hi

My node.js skills when it comes to async functions, leaves much to be desired. Below is a copy paste exercise from some code, but I can just not figure out how to return the result from this function call:

```auto
function authenticateRequest(req, res, next) {
        var request = new Request(req);
        var response = new Response(res);

       return oauth.authenticate(request, response)
                .then(function(token) {
                        next();
                }).catch(function(err) {
                        msg.res.status(err.code || 500).json(err);
                });
}

```

I know the return inside the function should not be there, that is about all. I would appreciate some assistance.

Regards  
Morne

---

<div class="post-metadata">

### Author: ![msupra](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/msupra/32/34760_2.png) [@msupra](https://discourse.nodered.org/u/msupra)
#### Post date: [5 January 2021 12:01 UTC](https://discourse.nodered.org/t/struggling-with-async-function/38574/2 "2021-01-05T12:01:15Z")

</div>

Figured it out:

```auto
function authenticateRequest(req, res, next) {
        var request = new Request(req);
        var response = new Response(res);

        oauth.authenticate(request, response)
                .then(function(token) {

                        msg.statusCode=200
                        msg.payload="Congratulations, you are in a secret area!"
                        node.send(msg)
                }).catch(function(err) {
                        
                        msg.statusCode=err.statusCode
                        msg.payload=JSON.stringify(err.message)
                        node.send(msg)
                })
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [19 January 2021 12:01 UTC](https://discourse.nodered.org/t/struggling-with-async-function/38574/3 "2021-01-19T12:01:18Z")

</div>

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