javascript - Bot has to restart to pass user input to request function -


i have api looks similiar inside node.js skype bot

var searchname = '', taxbillnu = '' _rows = '10', searchdetail;  function getmobiledata (name, taxbill, rows) { url = "http://example.com/api/search/ownername="+name+"&taxbill="+taxbill+"&rows="+rows; request({     url: url,     json: true }, function (error, response, body) {         if (!error && response.statuscode === 200) {          searchdetail = body;          }else{             session.begindialog('/');              err = error;           } }) } 

once user gets rename ask them type in search query , goes list of search results using api

bot.dialog('/rename', [     function (session) {         builder.prompts.text(session, "type search query..");     },     function (session, results) {         if (results.response) {             searchname = results.response;             getmobiledata(searchname, taxbillnu, _rows)             if(err){             session.send(err);             }             session.begindialog('/relist');         }     } ]); 

my problem far, though search results ( on second go around ) error @ first problem has occured, search variable saved function not update body of json request until after has restarted. how ensure can run function after bot.dialog('/rename', [ , updated searchdetail = body first time?

edit: @ point takes 3 restarts before url complete user inputs , passes me need.

use asynchronous programming techniques organize program control flow.

since need wait operation complete (rest api request), , pass data next step, you've got perfect use case async.waterfall() control flow pattern.

for example, using node.js module async, construct async.waterfall inside dialog handler this:

var async = require('async');  bot.dialog('/rename', [     function (session) {         builder.prompts.text(session, "type search query..");     },     function (session, results) {         if (results.response) {             searchname = results.response;             // async stuff here             async.waterfall([                 function(callback) {                     // modify getmobiledata() method returns desired result                     var mobiledata = getmobiledata(searchname, taxbillnu, _rows);                     callback(null, mobiledata);                 },                function(mobiledata, callback) {                    // mobiledata gets passed previous function                    // call session begindialog                    session.begindialog('/relist');                    callback(null);                }            ], function (err, result) {                // handle errors here                session.send(err);            });          }     } ]); 

Comments