Ich versuche, die Funktionen in meinem Messenger/wit.ai Chat-Bot von Callbacks zu Versprechen zu aktualisieren.Promise-Funktion löst nicht auf (javascript/Wit.ai)
Dieses ursprüngliche Format führt fein:
['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)
},
Aber wenn ich, wie unten zu Promises aktualisieren, es nicht durch machen:
['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)
})
},
Ich habe versucht, das Debuggen von Konsolenprotokollen Hinzufügen überall in der Funktion wie folgt aus:
W enn die Funktion ausgelöst wird, stoppt er auf halbem Weg durch und scheitert, das Versprechen zu lösen:
Wenn ich versuche, console.log (Kontext) innerhalb der Funktion I ‚undefined‘ erhalten.
Was fehlt mir?
EDIT: Wenn ich die geschweiften Klammern um meine Funktion Parameter wie so entfernen:
['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)
})
},
Ich bin in der Lage meinen Kontext einzuloggen, aber immer noch das Versprechen nicht lösen kann:
Können Sie versuchen, zu lösen (Kontext), ohne Return-Anweisung. – Neelesh
Keine Änderung, steht immer noch auf Punkt 4 und sendet dann eine 'no callback' Warnung. – wanstan
Haben Sie den Promise Polyfill hinzugefügt? Sie müssen, wenn Sie planen, auf Internet Exploder ausgeführt werden. –