Unexpected token error after adding await keyword to Node.js code

This topic was automatically generated from Slack. You can find the original thread here.

Neil Lovelace : Hello Everyone,
Fairly new to the world of javascript, but learning quickly. Anyone have an idea why I’m getting a syntax error on this line? It only occurs when I add the await

Dylan Sather (Pipedream) : Hi , getting used to async / await and asynchronous programming in JS can take a long time even for seasoned JS devs, so no sweat :sweat_smile: .

You’ll need to declare that function as async:

async function newToken(domain) { ...

Dylan Sather (Pipedream) : You’ll then want to call that function using:

await newToken(...)

Neil Lovelace : Thanks ! You’re always a wealth of knowledge

Dylan Sather (Pipedream) : :thumbsup: let us know if you have any other trouble

Neil Lovelace : Do I need to include await with my AXIOS.get()?

Neil Lovelace : I keep getting this:

This step was still trying to run code when the step ended. Make sure you await all promises.

Giao Phan : You’ll need to await your newToken call, and any async calls (like axios) within the newToken function.

Neil Lovelace : Thanks . I’m doing so, but continue to get this message.
Now I’m getting a 401 error using await AXIOS…

Dylan Sather (Pipedream) : would you mind sharing your workflow with dylan@pipedream.com? I can take a look at the Promise warning to see if there’s anywhere else you need to await.

Re: the 401, that suggests the request is unauthorized, so it’s likely the token you’re passing is either invalid, or it’s not being passed correctly. I’d suggest looking at the docs a bit more for that service to confirm you’re indeed forming the request correctly.

Neil Lovelace : Done.
I’m trying to create this on my own - I know that you’ve created a Procore connector already. I have to learn this so I can understand it better for my own benefit.

Dylan Sather (Pipedream) : got it, thanks. You’ll need to await checkToken('Procore');

Dylan Sather (Pipedream) : anywhere you define an async function, you’ll normally await that function when you call it

Neil Lovelace : I placed the await in front of checkToken but thats what is giving me the 401 error. Once I remove it, It seems to work fine.

Neil Lovelace : Very humbling…

Neil Lovelace : I figured this part out. Silly mistake on my part.
Next is returning the pro_token variable between steps. I feel like I’m assigning the value properly and I thought I was returning it in the correct place(s), but it continues to be undefined when I try to console.log it.

Dylan Sather (Pipedream) : it’s subtle but I believe you might not be getting the 401 error because the code isn’t even running correctly (if you’re not awaiting the function, Pipedream will just move on to the next step and so you may never make the HTTP request). You’ll want to add the await back in, and hunt down the source of the 401.

I’m not sure with Procore, but most OAuth access tokens have an expiry of ~60 minutes. Is it possible that the token you stored as an environment variable is old, and that’s why it’s failing? That would be my first guess.

Dylan Sather (Pipedream) : Also since it looks like you’re trying to catch that case - get a new token if the old one is expired - you’ll actually want to wrap the initial axios.get call where you check the token within a try / catch statement. You’re never trying to fetch a new token because axios will throw an error if it gets a 401. So you can’t just check for errors in the response like you are. Your code will never reach that point. You essentially want to:

try {
  await axios.get()
} catch (err) {
  await newToken(..._
}

Dylan Sather (Pipedream) : Try to read the axios docs to see how they throw on HTTP errors like this, and read up on try / catch a bit to see how to incorporate that logic into your code

Neil Lovelace : #LIFESAVER