Coder Social home page Coder Social logo

Comments (7)

Runrioter avatar Runrioter commented on May 5, 2024 1

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))
    })
  });

from compose.

Runrioter avatar Runrioter commented on May 5, 2024
await compose(stack)({});

Here, await compose(stack)({}) is wrong.

from compose.

xyluet avatar xyluet commented on May 5, 2024

Yes, so it should caught in try catch, but it didn't.

from compose.

Runrioter avatar Runrioter commented on May 5, 2024

Try this await expect(compose(stack)({})).rejects.toThrowError('next() called multiple times'); ?

@xyluet

from compose.

xyluet avatar xyluet commented on May 5, 2024
it(`should throw if next() is called multiple times in non async function`, async () => {
    const stack = [];

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

    await expect(compose(stack)({})).rejects.toThrowError('next() called multiple times');
  });

I tried that before, it resolved to value, but still throw error in UnhandledPromiseRejectionWarning

expect(received).rejects.toThrowError()

    Expected received Promise to reject, instead it resolved to value
      undefined

from compose.

fl0w avatar fl0w commented on May 5, 2024

expect doesn't return a promise, so the last example wouldn't work. koa-compose doesn't support sync calls, thus your last next() occurs out of context and the throw isn't caught.

It's the same as doing

try {
  Promise.reject()
} catch {
  // nothing to catch, nothing thrown yet
}

from compose.

fl0w avatar fl0w commented on May 5, 2024

@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.

from compose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.