0

I am learning about the Promise api in javascript and would like to apply it to the following: Consider the javascript code below that outputs the content of an rss feed using google feeds (from http://www.javascriptkit.com/dhtmltutors/rssdisplayer.js)

google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function rssdisplayer(divid, url, feedlimit, showoptions) {
    this.showoptions=showoptions || "" //get string of options to show                 ("date" and/or "description")
    var feedpointer=new google.feeds.Feed(url) //create new instance of Google Ajax Feed API
    feedpointer.setNumEntries(feedlimit) //set number of items to display
    document.write('<div id="'+divid+'">Loading feed...</div>')
    this.feedcontainer=document.getElementById(divid)
    var displayer=this
    feedpointer.load(function(r){displayer.formatoutput(r)}) //call    Feed.load() to retrieve and output RSS feed
}

rssdisplayer.prototype.formatdate=function(datestr){
     var itemdate=new Date(datestr)
     return "<span style='color:gray; font-size:    90%'>"+itemdate.toLocaleString()+"</span>"
}


rssdisplayer.prototype.formatoutput=function(result){
    if (!result.error){ //if RSS feed successfully fetched
      var thefeeds=result.feed.entries //get all feed entries as a JSON  array
      var rssoutput="<ul>"
      for (var i=0; i<thefeeds.length; i++){ //loop through entries
          var itemtitle="<a href=\"" + thefeeds[i].link + "\">" +             thefeeds[i].title + "</a>"
          var itemdate=/date/i.test(this.showoptions)?              this.formatdate(thefeeds[i].publishedDate) : ""
          var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
         rssoutput+="<li>" + itemtitle + " " + itemdate + itemdescription + "</li>"
      }
     rssoutput+="</ul>"
     this.feedcontainer.innerHTML=rssoutput
  }
  else //else, output error
   alert("Error fetching feeds: "+result.error.message)
}

The rss feed is retrieved and displayed with the call

new rssdisplayer("adiv", "somerssurl", 5, "date, description")

How can I change the callback formatoutput to return a Promise (using say new Promise(function resolver...) and use it after creating a rssdisplayer object? The goal is to be able to synchronize on the feed load so that tasks dependent on this can then do something.

user35202
  • 389
  • 1
  • 10
  • What does feedpointer.load do? Does that return a promise? – mbx-mbx Mar 09 '16 at 16:18
  • 1
    You need to tell us what existing function you want to replace with something that returns a promise. I only see one potentially async operation in your code that looks like a candidate for a promise and that is `feedpointer.load()` which you do not show us the code for. Is that what you want to turn into a promise? Also, you can't return a promise from an object constructor because the constructor must return the newly created object. – jfriend00 Mar 09 '16 at 16:21
  • 1
    @jfriend00 Just looked at the google docs for the feedpointer.load() it doesnt return a promise, it only accepts a callback. Only thing i can think of is create a deferred in the constructor, return that, pass it into the formatoutput and resolve it when that function ends. Sounds messy though and I wouldnt actually bother. – mbx-mbx Mar 09 '16 at 16:24
  • That API is deprecated for a while now, I'd suggest a diff approach. – Jim Edelstein Mar 09 '16 at 16:25
  • @Magrangs - Yeah, I can't figure out what objective the OP is trying to accomplish. – jfriend00 Mar 09 '16 at 16:25
  • Think it's a learning exercise. But its not a great one to start with. – mbx-mbx Mar 09 '16 at 16:26
  • @Magrangs It has no return value (from the official page [link](https://developers.google.com/feed/v1/reference#feed) – user35202 Mar 09 '16 at 16:28
  • @user35202 Yeah I saw, to be honest, this is not a worthwhile exercise for this scenario. – mbx-mbx Mar 09 '16 at 16:30
  • @jfriend00 feedpointer.load is Google Feed api so out of my control [link] (https://developers.google.com/feed/v1/reference#feed) – user35202 Mar 09 '16 at 16:30
  • @Jim Edelstein It works fine (I have tried it). – user35202 Mar 09 '16 at 16:32
  • @jfriends Yes it is a learning exercise. I basically want the async call to return a promise so that functions dependent on the feed load can then do something. – user35202 Mar 09 '16 at 16:34

0 Answers0