Saturday 20 May 2017

Need help with Yield and Generator functions

I'm still trying to grasp the concept of generator functions and yield so bear with me.I'm using Adonis.js and things are going well for the most part, but I've hit a snag. I'm trying to ajax call to a url, which then uses the Plivo library to send an SMS, then returns the result (if it sent or not). The code is executing and Plivo is sending the sms and console.logging out the result, but the output json is being returned to the front end before Plivo is finished.So my question is: how do I wait for Plivo to finish sending the SMS before returning the json to the front end. Adonis allows me to yield a DB call, so the code "waits" for the DB result before continuing, but if I yield the Plivo call:var output = {} yield Plivo.send_message(params, function(status, response) { if (status === 202) { output.status='success'; console.log('success'); } else { output.status='fail'; console.log('fail'); } }) return response.json(output); I get an error about "You may only yield a function, promise, generator, array, or object, but the following object was passed: "undefined""Any help would be greatly appreciated.

Submitted May 21, 2017 at 03:06AM by svenjoy_it

No comments:

Post a Comment