seantrant
10/4/2017 - 3:23 PM

Callbacks - Function with responses eg error and resposnse

This is how to return values back to a function

// call function with a function as a parementer 

Service.sendDirectionToLaser(this.laserIP, {x:20, y:20} , (err, res) => {
  console.log(err, res)
})
  
// use this function to return the err & res values

sendDirectionToLaser(laserIP, offset, fnc) {
  return request.post(`/laserDirection`)
    .set('Content-Type', 'application/json')
    .send({"laserIP": laserIP ,"offset": offset})
    .end(function (error, response) {
      if(error){
        console.log( ' it was an error' , error)
        fnc(error)    // this is returning a value for 'err'
      }else{
        console.log('its all good g')
        fnc(null, response) // this is returning a value null for 'err' and reponse for 'response'
      }
     })
}