I'm trying to update the functions in my messenger/wit.ai chat bot from using callbacks to promises.
This original format executes fine:
['buildScenario'](sessionId, context, cb) {
var trendChoice = scenarioCombos['trends']
var disruptionChoice = scenarioCombos['disruptions']
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
cb(context)
},
But when I update to Promises as below, it doesn't make it through:
['buildScenario']({sessionId, context, entities}) {
return new Promise(function(resolve, reject) {
var trendChoice = scenarioCombos['trends']
var disruptionChoice = scenarioCombos['disruptions']
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
return resolve(context)
})
},
I've tried debugging by adding console logs throughout the function like this:
When the function is triggered, it stops halfway through and fails to resolve the promise:
When I try console.log(context) within the function I get 'undefined'.
What am I missing?
EDIT: When I remove the curly brackets around my function parameters like so:
['buildScenario'](sessionId, context, entities) {
console.log('BS POINT 1')
return new Promise(function(resolve, reject) {
console.log('BS POINT 2')
var trendChoice = scenarioCombos['trends']
console.log(trendChoice)
console.log('BS POINT 3')
var disruptionChoice = scenarioCombos['disruptions']
console.log(disruptionChoice)
console.log('BS POINT 4')
console.log(context)
context.trend = trendChoice[Math.floor(Math.random() * trendChoice.length)]
console.log(context)
console.log('BS POINT 5')
context.disruption = disruptionChoice[Math.floor(Math.random() * disruptionChoice.length)]
console.log(context)
console.log('BS POINT 6')
return resolve(context)
})
},
I'm able to log my context but still can't resolve the Promise: