-3

While the following code works great to call a python script and get the output:

s = spawn 'python', ['-u', 'foo.py']
s.stdout.on 'data', (data) -> msg.send data.toString()
s.stderr.on 'data', (data) -> msg.send data.toString()

foo.py returns many different responses (it returns updates as it runs).

For instance:

def function1():
  print "Function 1 complete"

def function2():
  print "Function 2 complete"

function1()
function2()

Hubot does not display those results in a consistent order. I know this can happen if msg.sendis called multiple times.

While I know I could re-write foo.py to behave differently, other processes depend on foo.py and I can't alter its behavior.

I was wondering what the process might be to collect the responses as they come and send a single msg.send in hopes that a single call to msg.send will preserve the order of the process outputs.

Achraf JEDAY
  • 1,936
  • 5
  • 24
  • 33
schroeder
  • 533
  • 1
  • 7
  • 25

1 Answers1

1

Imho, best would be to use promises, by the addition of q or bluebird

It would lead, for example, to:

Promise.all([function1(), function2()]).then (res) ->
  msg.send res.join(", ")
Mose
  • 876
  • 7
  • 7