Would like to get the most recent value from a deferred object

6 views
Skip to first unread message

Steve Thomas

unread,
Oct 4, 2013, 3:43:54 PM10/4/13
to defer...@googlegroups.com
Hi -

I was previously looking into Async and was fortunate enough to stumble upon your library and like what I'm seeing in the docs but can't seem to find this use case spelled out clearly.

I'm making use of https://github.com/husanu/nodejs-cpu-usage, and running it every 10 seconds to get the latest value.  Elsewhere in the same file, I run a loop every 30 seconds and just want to pull the latest value from cpu-usage.  Currently, my code looks like

var readCpu = deferred.promisify(require("cpu-usage"));
var last_cpu = 0;
readCpu(10000, function(load) {
   last_cpu = load;
});

var HEARTBEAT_INTERVAL = 30*1000;
temporal.loop( HEARTBEAT_INTERVAL, function() {
   ...
   results.cpu = last_cpu;
   ...
}

when really I would just like to do something like

readCpu(10000);
var HEARTBEAT_INTERVAL = 30*1000;
temporal.loop( HEARTBEAT_INTERVAL, function() {
   ...
   results.cpu = readCpu.value();    // reflects last value returned
   ...
}

So, I'm not sure if I'm using the deferred semantics as intended, or if I'm missing something already supported, or if I'm introducing something new to the mix.  I'd be grateful for some help.  

Thanks very much,

Steve

Mariusz Nowak

unread,
Oct 5, 2013, 12:59:07 PM10/5/13
to defer...@googlegroups.com
Hi Steve,

It can't work well as `cpu-usage` is not an async function, and promisify is made to wrap async functions as ones you can find in node API (async function calls its callback only *once*, and *passes eventual error as first argument and success value as second*)

I think it's also not valid use case from promises. Promises are about asynchronous operation that resolve once, while observing CPU usage changes is about handling some kind of interval. I think you should just do it the way you've done in your first snippet.

/Mariusz

Steve Thomas

unread,
Oct 7, 2013, 8:26:36 AM10/7/13
to defer...@googlegroups.com
Thanks very much for the clarification, Mariusz!  

Steve


--
You received this message because you are subscribed to a topic in the Google Groups "deferred-js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/deferred-js/6Tv2cVQywCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to deferred-js...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages