-2

I am having some difficulties with node.js & using functions. My problem is when I call a function, node.js doesnt wait for it to finish and I ended up getting nothing as my return values. This is what I have

exports.handle = (event, context,callback) => {
switch (event.request.type)
{
    case "IntentRequest":
    console.log('INTENT REQUEST')    
    switch(event.request.intent.name)
    {
       case "MoveDown":
            readpos()
    }
}}
function readpos(){
  var position = []
 //this code parses an array and gets me x y values
 return position }

My problem is I end up getting an empty array because node.js runs to fast. Im assuming I have to do something with callback but im unsure on how to implement callback. I've tried reading online tutorials on callback but all of them have confused me as well and I cant seem to apply what online resources say to my cold. My main language is c++ & Python.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Continuum
  • 67
  • 1
  • 9
  • Possible duplicate of [How to make a function wait until a callback has been called using node.js](https://stackoverflow.com/questions/5010288/how-to-make-a-function-wait-until-a-callback-has-been-called-using-node-js) – Kevin B Sep 22 '17 at 21:06
  • I've seen that thread and it confused me even more. – Continuum Sep 22 '17 at 21:07
  • That fortunately doesn't make it any less of a duplicate. – Kevin B Sep 22 '17 at 21:08
  • I mean I tried to do what they said as in change my readPos() to readPos(function(err,data)//compute data callback(data))) however that still didnt work for me – Continuum Sep 22 '17 at 21:20
  • readPos doesn't accept a callback as it is currently coded. – Kevin B Sep 22 '17 at 21:22

2 Answers2

0

It's quite simple. You handle it in your callback. Let's look at your fixed function:

exports.handle = (event, context,callback) => {
  switch (event.request.type)
  {
      case "IntentRequest":
        console.log('INTENT REQUEST'); 
        switch(event.request.intent.name)
        {
           case "MoveDown":
             callback(readpos());
        }
  }
};
function readpos() {
  var position = [];
 //this code parses an array and gets me x y values
 return position;
}

And now, when you call the handle, you just call it as such:

handle(event, context, 
// This is your callback function, which returns when done
function(position){
   // When readPos() has run, it will return the value in this function
   var newPos = position + 1; ...etc...
});

Of course, your code should follow conventions. Callbacks are designed to return errors and results so you should cater for that too. But this is just a general idea of callbacks :)

SalGad
  • 2,991
  • 2
  • 18
  • 25
0

You need to use a callback as

exports.handle = (event, context,callback) => {
switch (event.request.type)
{
    case "IntentRequest":
    console.log('INTENT REQUEST')    
    switch(event.request.intent.name)
    {
       case "MoveDown":
            callback();
     }
}}

Usage

function readpos(){
  var position = []
 //this code parses an array and gets me x y values
 Don't return here instead use the result directly or store into a          global
  }





handle(event,context,readpos);
Elias Bundala
  • 144
  • 1
  • 5