Message from JavaScript discussions
November 2017
— Can you explain? I am really interested
function useOldEntry(oldEntry) {
entryEmitter.once('entry', function (entry) {
// access to oldEntry and entry variables
return useOldEntry(entry);
});
}
useOldEntry(null) // no old entry on startup
— This way, for each iteration, you have access to current and previous version of an "entry"
— Caveats:
.once may be slow
useOldEntry HAS to be called synchronously within the callback to avoid possibility of skipping entries
— Pros:
Pure functional ish
Gets the job done
— Alright maybe I need to see this in action in my actual code to see the picture
— But, ehh, monads may be nicer
— Because, yes the concept seems doable
— But I can't see the spot to fit It
— I have an actual nodejs example
— A HTTP server that increments a counter without modifying state
—
function count(i) {
server.once('request', function (req, res) {
res.end(String(i));
return count(i + 1);
});
}
count(0);