This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.

NodeJS help, callback?

dennis91dennis91 Member
edited August 2017 in Questions & Help
Hi,

I'm trying to write a app for Homey to learn some NodeJS. The beginning is working, but for whatever reason i don't understand how the callback in my main function works.

This is the example code i have :

var parser = require('rss-parser');

function init() {
        Homey.log("Tweakers app running!");

        Homey.manager('flow').on('action.tweakers',function(callback,args){
                console.log("Debug"+getLatestNews(callback));
        });

}

var testarray = []
function getData(value){
        testarray.push(value['title']);
        return(testarray);
}
function execute(someFunction, value) {
return(someFunction(value));
}

function showLatest(array){
        console.log("Latest one:"+array[0]);
}



function getLatestNews(callback){

var parser = require('rss-parser');
var dataArray = "";
parser.parseURL('<feed>', function(err, parsed) {
  parsed.feed.entries.forEach(function(value){ dataArray = execute(getData, value);
 })
callback(dataArray[0]);
})
Zoals jullie zien probeer ik een feed uit te lezen. Dat gaat goed, zodra ik in de getLatestNews functie een Homey.log zet krijg ik netjes de output die ik wil.
Maar zodra ik vanuit de flow editor dit trigger gaat de flow action af, en krijg ik een undefined terug, ik denk omdat ie dus niet netjes async wacht op het resultaat van getLatestNews(). Iemand hier tips hoe ik verder kom? Zit er al een tijdje mee te stoeien :open_mouth:

Comments

  • If you are working on a SDK1 app, i would suggest you start with SDK2.
    SDK2 works with promises (or async await) and works a lot better when you get used to it :smile:

    I think you could add the callback after:
     parsed.feed.entries.forEach(function(value){ dataArray = execute(getData, value);

    But indeed its an async issue :smile:

  • By the way, you can get allot of help on Slack :smile:
  • Link to get access : https://slack.athom.com/ :)

  • swttt said:
    If you are working on a SDK1 app, i would suggest you start with SDK2.
    SDK2 works with promises (or async await) and works a lot better when you get used to it :smile:

    I think you could add the callback after:
     parsed.feed.entries.forEach(function(value){ dataArray = execute(getData, value);

    But indeed its an async issue :smile:

    Yes, i would like to start on SDK 2. But i think i have to update my Homey to experimental firmware then, right?

    swttt said:
    By the way, you can get allot of help on Slack :smile:
    Ah that is great. I will try that. Thanks!
Sign In or Register to comment.