question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple next with non async function

See original GitHub issue

Hi,

I wrote test like below, it throw an error but it come like this

UnhandledPromiseRejectionWarning: Error: next() called multiple times

and the test failed. Is this expected?

  it(`should throw if next() is called multiple times in non async function`, async () => {
    const stack = [];

    stack.push((ctx, next) => {
      next();
      next();
    });

    expect.hasAssertions();

    try {
      await compose(stack)({});
    } catch (error) {
      expect(error).toBeInstanceOf(Error);
    }
  });

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Runriotercommented, Dec 7, 2018

No, koa-compose support sync calls. The problem here is next() return a Promise. We need return Promise or include it in async function (had been included in test case).

The following one is use promise-based sync function.

  it(`should throw if next() is called multiple times in non async function`, () => {
    const stack = [];

    stack.push((ctx, next) => {
      return next().then(() => {
        return next()
      });
    });

    return compose(stack)({}).then(() => {
      throw new Error('boom')
    }, (err) => {
      assert(/multiple times/.test(err.message))
    })
  });
0reactions
fl0wcommented, Dec 7, 2018

@Runrioter that’s exactly what I ment (I maybe expressed it badly). If you’re dealing with them as promises the request chain isn’t synchronous, the execute on a new tick.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using "await" inside non-async function - Stack Overflow
My problem is I'd like the synchronous function to be able to wait for a value from the async one... They can't, because:....
Read more >
JavaScript async and await - in plain English, please
JavaScript developers love using async-await . It is the most straightforward way to deal with asynchronous operations in JavaScript.
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration declares an async function where the await keyword is permitted within the function body. The async and await ...
Read more >
How to escape async/await hell - freeCodeCamp
While working with Asynchronous JavaScript, people often write multiple statements one after the other and slap an await before a function call.
Read more >
Asynchronous programming in C# | Microsoft Learn
An overview of the C# language support for asynchronous programming using async, await, Task, and Task.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found