Git Product home page Git Product logo

Comments (7)

pward123 avatar pward123 commented on May 27, 2024

In the example above asCallback is from https://www.npmjs.com/package/ascallback

from hemera.

StarpTech avatar StarpTech commented on May 27, 2024

If I understand your case correctly you want to support to pass promises to callback handler or return them inside a normal function?

When yes this is definitely wrong because there are only two types of asynchronous composition which are very common in javascript.

  • Callback style
  hemera.add({
    topic: 'test',
    cmd: 'boom',
    fail: Joi.boolean().required(),
  }, function (req, cb) {
    cb(<error>, <result>) // result can be any value, error must be an instance of Error
  })
  • Async / Await
  hemera.add({
    topic: 'test',
    cmd: 'boom',
    fail: Joi.boolean().required(),
  }, async function (req, cb) {
    const result = await Promise.resolve()
    return result // can be a promise or any other value
  })
  • Pass promise as value

Some others support this syntax but we don't provide a reply interface in an act or add

  hemera.add({
    topic: 'test',
    cmd: 'boom',
    fail: Joi.boolean().required(),
  }, async function (req, reply) {
       reply.send(Promise.resolve())
  })

This is possible in hemera.

      hemera
        .act(
          {
            topic: 'math',
            cmd: 'add',
            a: 1,
            b: 2
          })
          .then(..)
          .catch(..)

Could I answer your question?

from hemera.

StarpTech avatar StarpTech commented on May 27, 2024

You have to decide between callback or async / await don't mix them.

from hemera.

pward123 avatar pward123 commented on May 27, 2024

Yep, if you want to use promises with hemera, return an async function or use asCallback.

from hemera.

StarpTech avatar StarpTech commented on May 27, 2024

Could I answer your question?

from hemera.

pward123 avatar pward123 commented on May 27, 2024

Yes, I believe so. Thanks.

I've always been under the impression that (from the callers perspective) both of the following just return a promise. I guess there are some subtleties that I haven't ran into until now.

async () => {
  return await myPromise()
}

() => {
  return myPromise()
}

from hemera.

StarpTech avatar StarpTech commented on May 27, 2024

No, this is quite uncommon.

from hemera.

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.