Randster
Randster

Reputation: 298

sails.js http.get and callback functions?

I'm new to Javascript async and Sails.js and I feel like I'm mis-understanding some concepts.

I have a function that is trying to make an HTTP GET call to get comments from a URL as JSON, then parse those comments to come up with a 'score' object to rate the users comments. An example of my code is below --

fn: async function (inputs, exits) {
let commentsUrl = 'http://foobar.com';

// we want to return a score of the users comments
let score = {};

// callback function that calls a helper to score comments
let processComments = async function (error, result) {
  let scored = await sails.helpers.scoreComments(result);
  return scored;
}

let HTTP = require('machinepack-http');

// go get JSON data of user comments
HTTP.get({
  url: commentsUrl,
  data: {},
  headers: {}
}).exec((err, result) => score = processComments(err, result)
);

// this should be awaiting for the processComments callback to finish?
return exits.success(score);

My problem is probably obvious - the function returns immediately before the callback has finished executing and the value of score is just {}. I have tried in sails to changing the HTTP.get call to --

// go get JSON data of user comments
HTTP.get({
  url: commentsUrl,
  data: {},
  headers: {}
}).exec((err, result) => score = await processComments(err, result)
);

But this results in an compiled error stating that sails doesn't understand the 'await' keyword in front of the processComments callback.

What am I mis-understanding? I would like to return the value of 'scored' back to the main function and feel that I should be 'await'ing something, but I'm not sure where.

Upvotes: 0

Views: 253

Answers (2)

Thiago Barcala
Thiago Barcala

Reputation: 7323

The await keyword works only with promises, and from the documentation of the library you are using (machinepack-http), it doesn't seem like they work with promises.

You could try to switch to a Promise-based library, like axios, or transform the requests of machinepack into Promises, like this:

function getComments() {
    return new Promise((resolve, reject) => {
        HTTP.get({
            url: commentsUrl,
            data: {},
            headers: {}
        }).exec((err, result) => {
            if (err) {
                return reject(err);
            }
            resolve(result);
        });
    });
}

Then you can use this inside fn:

score = processComments(await getComments());

return exits.success(score);

Upvotes: 1

Randster
Randster

Reputation: 298

It occurred to me what I should be doing is the response in the callback --

fn: async function (inputs, exits) {
let commentsUrl = 'http://foobar.com';

// callback function that calls a helper to score comments
let processComments = async function (error, result) {
let scored = await sails.helpers.scoreComments(result);
  return return exits.success(scored);
}

let HTTP = require('machinepack-http');

// go get JSON data of user comments
HTTP.get({
 url: commentsUrl,
 data: {},
  headers: {}
}).exec((err, result) => score = processComments(err, result)
);

// return nothing here
}

Upvotes: 0

Related Questions