最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing arguments to a function using asyncawait - Stack Overflow

programmeradmin7浏览0评论

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

// mon.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 './mon'

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 es from when testcafe chrome client/__tests__/ is run, rather than being imported in the mon.js file.

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

// mon.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 './mon'

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 es from when testcafe chrome client/__tests__/ is run, rather than being imported in the mon.js file.

Share Improve this question edited Jul 16, 2019 at 14:57 Alex Skorkin 4,2743 gold badges26 silver badges48 bronze badges asked Jul 17, 2017 at 15:25 dacedace 6,37314 gold badges47 silver badges79 bronze badges 6
  • t is thus a dependency? – Sumi Straessle Commented Jul 17, 2017 at 15:29
  • Where is t defined? – Aron Commented Jul 17, 2017 at 15:29
  • 5 Well, you haven't defined t, so it's not defined, which is what I think the error message is actually saying. – Heretic Monkey Commented Jul 17, 2017 at 15:29
  • myAsyncFunction(t, await textA, await textB) ? – Jonas Wilms Commented Jul 17, 2017 at 15:30
  • @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). – Heretic Monkey Commented Jul 17, 2017 at 15:33
 |  Show 1 more ment

1 Answer 1

Reset to default 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 ments.

发布评论

评论列表(0)

  1. 暂无评论