简体   繁体   中英

Replacement async-await to then

I tried to remove async await from code

before(async () => {
  await tests.env();
  token = await tests.getToken(accMock, 'acceptor');
});

My attempt:

tests.env()
  .then((output) => output.getToken(accMock, 'acceptor')
  .then((v) => (token = v)));

But this code does not pass the tests. What can be wrong?

The two pieces of code are not equivalent. Your first piece of code is:

before(async () => {
  await tests.env();
  token = await tests.getToken(accMock, 'acceptor');
});

Your second piece of code, rewritten with async/await is:

before(async () => {
  let output = await tests.env();
  let v = await output.getToken(accMock, 'acceptor');
  token = v;
});

Note that in the first code you're calling tests.getToken() but in the second you're calling output.getToken() .

The correct re-write is:

before(() => {
  return tests.env()
              .then(() => tests.getToken(accMock, 'acceptor'))
              .then(v => token = v);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM