Error: function uses multiple asynchronous interfaces: callback and promise
See original GitHub issueHello Everyone, I have a case where I am unable to use async in the step definitions. I have a simple scenario where in am logging in through the api interface of my application to fetch auth token My feature file is:
Feature: Login
Scenario: Login using api
Given I login to my account with my username and password
Then I should get an authorization token
|username|password|
|xyz@email.com|dev|
my steps are :
defineSupportCode(({ Given, Then, setDefaultTimeout }) => {
const timeOut = 30000;
const baseSteps: BaseSteps = new BaseSteps();
setDefaultTimeout(timeOut);
// tslint:disable-next-line:only-arrow-functions
Given(/^I login to my account with my username and password$/, async (table: TableDefinition) => {
const userData = table.hashes();
const loginResponse = await baseSteps.loginUser(userData[0].username, userData[0].password);
console.log('Login response is ', loginResponse);
const statusCode = 302;
expect(await loginResponse.status).to.equal(statusCode);
});
Then(/^I should get an authorization token$/, async () => {
const authorizationHeader = await baseSteps.getAuthorizationHeader();
console.log('Auth Header', authorizationHeader);
expect(authorizationHeader).to.not.equal(null);
const orders = await fetch('url',
{
method: 'GET', headers: {
authorization: authorizationHeader
}
});
// tslint:disable-next-line:no-console
console.log(await orders.json());
});
});
And my helper functions in the BaseSteps.ts are as follows:
async loginUser(userName: string, password: string): Promise<Response> {
const getSignInFormResponse = await fetch(this.getInitialRequestUrl(),
{
method: 'GET'
});
const form = <some-form-data>;
const loginResponse = await fetch(getSignInFormResponse.url,
{
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
body: form,
redirect: 'manual'
});
return loginResponse;
}
async getAuthorizationHeader() {
const tokenResponse = await fetch(this.getInitialRequestUrl(),
{
method: 'GET',
redirect: 'manual'
});
const tokenInfo = qs.parse(tokenResponse.headers.get('location'));
const authorizationHeader = `${tokenInfo.token_type} ${tokenInfo.access_token}`;
return authorizationHeader;
}
When i run my scenario it fails with the following error:
Error: function uses multiple asynchronous interfaces: callback and promise
on the line where my Given starts
Please help me fix this error.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
function uses multiple asynchronous interfaces Promise ...
Error : function uses multiple asynchronous interfaces: callback and promise to use the callback interface: do not return a promise to use ...
Read more >cucumber/cucumber-js - Gitter
Hey guys!, do you know if cucumberjs has something like callback.fail() ? ... Error: function uses multiple asynchronous interfaces: callback and promise ....
Read more >How to rewrite a callback function in Promise form and async ...
First, we remove the callback argument. Then we add the code to return a new Promise from our Promise-based function. The error callback...
Read more >Using promises - JavaScript - MDN Web Docs
In the old days, doing several asynchronous operations in a row would ... failureCallback) — so if your error handling code is the...
Read more >Asynchronous Programming - Eloquent JavaScript
This is the main advantage of promises—they simplify the use of asynchronous functions. Instead of having to pass around callbacks, promise-based functions look ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your
Givenstep definition has atableargument while in your feature file, it does not have a table. Cucumber-js determines if you are using thecallbackinterface based on the number of arguments in your step definition. If the number is one more than the number of arguments that are passed in, then it assumes you are using the callback interface. Since you are also returning a promise, it determines you are using the promise interface. Only one interface at a time can be used.Thoughts on updating the error message to include something about the fact that the callback interface is assumed because the step definition has X number of arguments?
Glad its working for you. I’ll open a separate issue for updating the error message to hopefully make this easier to figure out in the future