How can I wrap my code in a Promise?

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

Franklin Bindels : Running into issues running asynchronous code, can anybody help me wrap this up into promises properly? https://pipedream.com/@michaelblufetch/fetch-from-ididata-p_WxCqk1d

Franklin Bindels : I get this:

ACTIVE_HANDLEThis step was still trying to run code when the step ended. Make sure you promisify callback functions and await all Promises. (Reason: GetAddrInfoReqWrap, Learn more: https://pipedream.com/docs/workflows/steps/code/async)

Franklin Bindels : It’s a 100% me for sure,

Dylan Sather (Pipedream) : Can you add an “await” before performSearch?

Also I would recommend creating a new client secret for the app. Once you’ve made that public, anyone can technically issue tokens on your behalf and it’s always a recommended practice to rotate it. I recently wrote up a security guide for Pipedream that discusses environment variables and other things that may be helpful: https://pipedream.com/docs/privacy-and-security/best-practices/

Franklin Bindels : AH for sure thanks for the heads up

Franklin Bindels : how would I add await before performSearch? it says i can’t add it in an asynchronous function

Dylan Sather (Pipedream) : Oh sorry I see now you chain the then method so that’s probably not the issue. Mind sharing the workflow with dylan@pipedream.com and I can take a deeper look?

Franklin Bindels : 100% man thanks so much

Franklin Bindels : gotta admit it’s a little over my head this one

Dylan Sather (Pipedream) : no worries, I made just a few modifications. The workflow is throwing a 403 error now, but the Promise / async-related errors should be corrected.

res.text() returns a Promise, so you’ll want to await all instances of that (see the node-fetch docs). Where you were calling performSearch recursively on line 81, you were also missing an await there, so that could have caused an issue. I also modified the then / catch method chaining at the bottom to use await, instead, which I personally find more readable (you can use try / catch instead, if you’d like to handle those errors in a custom way).

Let me know if that helps

Franklin Bindels : THat def helps thanks, 403 error is probably authentication right

Dylan Sather (Pipedream) : That’s right

Franklin Bindels : They shouldve whitelisted the ip, leaves the secret and id then?

Dylan Sather (Pipedream) : yes if a destination doesn’t whitelist the IP, you’ll typically see a TIMEOUT error since you can’t actually reach the destination host. So something is either off about the client / tokens themselves, or you’re not passing them exactly how they expect

Franklin Bindels : yeah i copied most of their code

Franklin Bindels : they’re saying they can authenticate with the same data and that it’s probably the proxy,

Franklin Bindels : They also asked for the error message but I only have the message that pipedream gives me

Dylan Sather (Pipedream) : I tried a few more things to get it to work but I didn’t have much luck. One thing I noticed is that fetch doesn’t accept the proxy via the httpsAgent property. You’ll need to pass that as agent. See GitHub - node-fetch/node-fetch: A light-weight module that brings the Fetch API to Node.js

Dylan Sather (Pipedream) : so currently the request may not be going through the proxy, and the 403 would make sense in that case

Franklin Bindels : ah i was gonna switch to axios like in the example,