Currently, I am in the process of updating the functions in my messenger/wit.ai chat bot to transition from using callbacks to promises.
The initial format functions properly:
['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)
},
However, after updating to Promises as shown below, it encounters issues:
['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 have attempted debugging by adding console logs within the function like this:
https://i.sstatic.net/FCdHL.png
Upon triggering the function, it halts midway and fails to fulfill the promise:
https://i.sstatic.net/vB3Jo.png
When I try console.log(context) inside the function, 'undefined' is returned.
Is there something I overlooked?
EDIT: When I remove the curly brackets around my function parameters as follows:
['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 can log my context successfully but still encounter resolution issues with the Promise: