1

I was working on custom unity cloud build integration for our team's slack channel. And got stuck with making proper callback. It looks like returning null before the asyc task being completed, So need some help in making proper async callback. I've included the script and console.log result.

var request = require('request');
var config = require('../../config.js');
var slackManager = require('../slack/slackmanager.js');

module.exports = {
    AlertSlack: function (payload) {
        var getLink = GetLink(payload);
        console.log(getLink.next());
        slackManager.alertSlack(getLink.next().value);
    }
}

function* GetLink(payload) {
    try {
        var shareEndPointURL = config.unity.apibaseurl + payload.links.api_self.href + "/share";
        var shareID = yield HttpRequestHandler(shareEndPointURL, 'GET');
        
        //space to yield more httpRequest
        //space to yield more httpRequest
        //space to yield more httpRequest
        
        return " some final data ";
    } catch (e) {
        console.log("Exception : " + e);
        return e;
    }
}

function HttpRequestHandler(endpointURL, httpMethod) {
    var options = {
        url: endpointURL,
        method: httpMethod,
        headers: config.unity.unityAuthHeaders
    }
    request(options, function (err, resp, body) {
       console.log(body);
       return body;
    });
}

And here's the console log

{ value: undefined, done: false }
<-----recived call in slackmanager.js with ### some final data ###----->
{"shareid":"dfgdfgszdffzc"}
Appslabs
  • 21
  • 3
  • 1
    Generators don't make asynchronous code magically synchronous. Better forget about them here. – Bergi Jun 02 '16 at 15:55
  • I'm not a java guy, I'm more inclined to C#. I would like to implement this for our team's game project. So any help or alternate method would be helpful. – Appslabs Jun 02 '16 at 16:03
  • Do it the hard way with callbacks first when you're still learning about JavaScript. – Bergi Jun 02 '16 at 16:04
  • Read this answer : http://stackoverflow.com/a/21846808/3866134 – Jose Hermosilla Rodrigo Jun 02 '16 at 16:05
  • @JoseHermosillaRodrigo Thanks for the link, I'll give a try and post the result. – Appslabs Jun 02 '16 at 16:12
  • Remember you have to return a Promise in HttpRequestHandler function to go with this approach. – Jose Hermosilla Rodrigo Jun 02 '16 at 16:17
  • @JoseHermosillaRodrigo : Sure, It looks like the only issue with my script is returning before the http callback. I have used "require-promise" package with this project and couldn't make it working. Do you know a better way to tackle this issue of promise or a link to proper usage of promise with node.js? – Appslabs Jun 02 '16 at 16:28
  • Node.js added Promise natively (I don't know since what version) so you don't need to use anoyher package if you don't want any extra functionality that includes those packages. You can visit MDN : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise . – Jose Hermosilla Rodrigo Jun 02 '16 at 16:37

1 Answers1

1

I finally found the solution, I reconsidered the logic and worked out simple event handlers.

Since I was already using an async callback successfully, it was pointless to make unwanted generators on top of it.

Here's the solution I've came up with. Much simpler and clean than the old version.

var request = require('request');
var config = require('../../config.js');
var slackManager = require('../slack/slackmanager.js');

module.exports = {
    AlertSlack: function (payload) {
        var shareEndPointURL = config.unity.apibaseurl + payload.links.api_self.href + "/share";    
        HttpRequestHandler(shareEndPointURL, "GET", HandleShareID);
    }
}

function HttpRequestHandler(endpointURL, httpMethod, handler) {
    var options = {
        url: endpointURL,
        method: httpMethod,
        headers: config.unity.unityAuthHeaders
    }
    request(options, function(error, response, body) {
        handler(JSON.parse(body));
    });
}

function HandleShareID(result){
    var downloadURL = config.unity.downloadEndPointBase + result.shareid;
    HttpRequestHandler(downloadURL, 'GET', HandleDownLoadedData);
}

function HandleDownLoadedData(result) {
    slackManager.alertSlack(result);
}
Appslabs
  • 21
  • 3