Нет необходимости объявлять функцию как async
, если вы не await
что-то внутри нее.
Вам нужно дождаться разрешения асинхронных функций. Чтобы сделать это (без async / await), вам нужно иметь доступ к обещанию, чтобы дождаться его разрешения. Итак, вам нужно вернуть обещание в методе createEnrollment
:
function createEnrollment(params) {
// Note the return before the fetch!
return fetch('URL')
.then(function(response) {
return response.json();
})
.then(function(text) {
var val = text;
console.log("VALUE " + val.userId)
return text;
});
Теперь в вашем тесте вы можете дождаться разрешения обещания:
describe('create enrollment', function () {
it('enroll the user into the system', function () {
// The return here is intended for the test to wait until the promise is fullfilled.
return createEnrollment(inputParams).then(function(x) {
console.log(x);
});
});
});
В качестве альтернативы, если вы используете асинхронный / ожидающий синтаксис:
async function createEnrollment(params) {
const response = await fetch('URL');
const text = await response.json();
return text;
}
describe('create enrollment', function () {
it('enroll the user into the system', async function () {
const x = await createEnrollment(inputParams);
});
});