1

I had some code working fine for a REST endpoint in which a message was:

  1. created in the database
  2. stepA was processed
  3. when stepA was ok, the response message was returned
  4. stepB was processed.

This was the code:

  // POST single message
  app.post('/message', (req, res) => {
    const url = req.body.properties.url
    const image = req.body.properties.image
    const extraField = req.body.properties.extraField
    db.message.create({
      url: url,
      image: image,
    })
      .then(() => myProcess(extraField, 'stepA'))
      .then(newMessage => res.json(newMessage))
      .then(() => myProcess(extraField, 'stepB'))
  })

Now I am trying to have the same using feathersjs, but I do not know how to do 2, 3, 4 exactly.

I have now an AFTER hook for the create method of the message service:

module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
  return function processNewMessage (hook) {

    const { extraField } = hook.data.properties
    Promise.resolve(myProcess(extraField, 'stepA'))
      .then( <<NO-IDEA>> ) // Send RESPONSE!!
      .then(() => myProcess(extraField, 'stepB'))

    return Promise.resolve(hook);
  };
};

So my question boils down to: How can I send the response and subsequently trigger 'myProcess stepB' using feathersjs?

Althoug this is 'legacy', I think it might still be relevant.

musicformellons
  • 12,283
  • 4
  • 51
  • 86

1 Answers1

0

It is answered in the FAQ of feathersjs! How to do processing after sending the response to the user:

It depends on the promise that you return in your hook. Here's an example of a hook that sends an email, but doesn't wait for a success message.

function (hook) {

  // Send an email by calling to the email service.
  hook.app.service('emails').create({
    to: 'user@email.com',
    body: 'You are so great!'
  });

  // Send a message to some logging service.
  hook.app.service('logging').create(hook.data);

  // Return a resolved promise to immediately move to the next hook
  // and not wait for the two previous promises to resolve.
  return Promise.resolve(hook);
}
musicformellons
  • 12,283
  • 4
  • 51
  • 86