Passing arguments to a function using async/await
Asked Answered
T

1

17

I'm trying to pass arguments to a function that uses async/await. I've defined my function like so

// common.js

export const myAsyncFunc = async (t, textA, textB) => {
  await t
    .typeText('#input-1', textA)
    .typeText('#input-2', textB);
};

However, when I try to import this function to another file, like so, I can't pass it t because t is not defined:

// index.js

import { myAsyncFunc } from './common'

myAsyncFunc(t, textA, textB)

Is it possible to just pass in my textA and textB arguments (possibly with currying or another way) with async/await?

EDIT: So this is being run as part of the test cafe library. It looks like t comes from when testcafe chrome client/__tests__/ is run, rather than being imported in the common.js file.

Tanyatanzania answered 17/7, 2017 at 15:25 Comment(6)
t is thus a dependency?Jaquenette
Where is t defined?Checkpoint
Well, you haven't defined t, so it's not defined, which is what I think the error message is actually saying.Klockau
myAsyncFunction(t, await textA, await textB) ?Meill
@Jonasw Yeah, I've never seen an error which states "is not undefined". I think the error is actually "is not defined", or "is undefined", because it's trying to run functions on t, which is undefined (at least from the little code provided).Klockau
Apologies, that was a typo. Should read is not definedTanyatanzania
H
10

You are importing/exporting myAsyncFunc, but in your code you are calling myAsyncFunction.

Also, you are chaining

.typeText('#input-1', textA)
.typeText('#input-2', textB);

But I think .typeText returns a promise, right? So you should:

export const myAsyncFunc = async (t, textA, textB) => {
  await t.typeText('#input-1', textA);
  await t.typeText('#input-2', textB);
};

Other than that, the code is working just fine, assuming you defined t somewhere, as pointed out in the comments.

Hentrich answered 17/7, 2017 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.