Jump to content

Wait for async calls


Recommended Posts

Hi,I hope you can point me in the right direction on this one.

 

I started out trying to build something with smartmobilestudio using a node server and a W3Json object.  My idea was to access the node server witch in turn despatches to different providers. For example the diggi story sample you provided for the W3Json class.

 

I changed the node server sample like so:   server.end('Hello world'); to server.end(mydiggiprovider.get(args);

 

in mydiggprovider a followed the sample for W3Json like so:

 

unit DiggiProvider; 

interface 

type 
  TDiggiProvider = class 
  public 
    function Get:String; 
    _data :String; 
  end; 

implementation 

uses 
  W3Inet, W3System; 

{ TDiggiProvider} 
function TDiggiProvider.Get() :String; 
begin 
  var mJSONP: TW3JSONP; 

  _data := 'No Data'; 
  mJSONP:=TW3JSONP.Create;   
  mJSONP.OnDataReady:= 
    Procedure (sender:TW3JSONP) 
    Begin 
      writeln(sender.data); 
      _data := sender.data;   // Here a want the call from node server to get notified so it can issue it's                                                      
      //server.end callback 
      // w3_showmessage(‘You got data!’); 
      mJSONP.free; 
    end; 
  mJSONP.Request('http://services.digg.com/stories/top?' 
    +'appkey=http%3A%2F%2Fmashup.com' 
    +'&type=javascript&callback='); 

  result := _data;     //This is of course not working, because I have no data yet, hence I need await                                         
                       // async or something like that. 
  end; 
end. 

 

Rgs  Per.

Link to post
Share on other sites

w3*** units cannot be used in node.js, because there is not browser DOM etc.

 

In node.js you can do a http request yourself using the node.js own httt api:

 
//start http server 
http.createServer( 
  procedure(request: JServerRequest; response: JServerResponse) 
  begin 
    var url := 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22' + 
      'http://www.reddit.com/r/javascript/.rss'; 

    //do a http GET request 
    http.get(url, 
      procedure(res: variant) 
      begin 
        var body = ''; 
        var resp := JClientResponse(res); 

        //append each chunk 
        resp.on('data', procedure(chunk: variant) 
        begin 
          body += chunk; 
        end); 

        //send response when all data is fetched 
        resp.on('end', procedure(dummy:variant) 
        begin 
          response.end(body); 
        end); 
      end); 
    end) 
  .listen(80, ''); 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...