<commit_before>'use strict'

const Hemera = require('./../packages/hemera')
const nats = require('nats').connect()

const hemera = new Hemera(nats, {
  logLevel: 'info'
})

hemera.ready(function () {

  //wrap add method
  let add = (pattern, cb) => {

    this.add(pattern, function (resp, next) {

      Promise.resolve(cb.call(this, resp)).then(function (result) {

          return next(null, result)
        })
        .catch(function (err) {

          return next(err);
        })
    })
  }

  //wrap act method
  let act = (pattern) => {

    return new Promise((resolve, reject) => {

      return this.act(pattern, function (err, res) {

        if (err) {
          return reject(err)
        }

        resolve(res)
      });
    });
  }


  add({
    topic: 'math',
    cmd: 'add'
  }, (resp, cb) => {

    return resp.a + resp.b
  })

  act({
    topic: 'math',
    cmd: 'add',
    a: 1,
    b: 20
  }).then(function (resp) {

    hemera.log.info('Result', resp)
  })
})<commit_msg>improve promise example, catch callback errors<commit_after>'use strict'

const Hemera = require('./../packages/hemera')
const Promise = require('bluebird')
const nats = require('nats').connect()

const hemera = new Hemera(nats, {
  logLevel: 'info'
})

hemera.ready(function () {

  //wrap add method
  let add = (pattern, cb) => {

    this.add(pattern, function (resp, next) {

      new Promise((resolve, reject) => {
          
          resolve(cb.call(this, resp))

        })
        .then((result) => next(null, result))
        .catch(next)

    })
  }

  //wrap act method
  let act = (pattern) => {

    return new Promise((resolve, reject) => {

      return this.act(pattern, function (err, res) {

        if (err) {
          return reject(err)
        }

        resolve(res)
      })
    })
  }


  add({
    topic: 'math',
    cmd: 'add'
  }, (resp, cb) => {

    return resp.a + resp.b
  })

  act({
    topic: 'math',
    cmd: 'add',
    a: 1,
    b: 20
  }).then(function (resp) {

    hemera.log.info('Result', resp)
  })
})
{'fraction_non_alphanumeric': 0.17270058708414873, 'fraction_numerical': 0.0029354207436399216, 'mean_word_length': 2.103186646433991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5859800', 'n_tokens_mistral': 732, 'n_tokens_neox': 687, 'n_words': 165}