This repository was archived by the owner on May 28, 2020. It is now read-only.
This repository was archived by the owner on May 28, 2020. It is now read-only.
Give arguments to the polling #1
Open
Description
It would be nice to be able to provide arguments to the polling function. For instance, we could write a polling to retrieve the private messages of a user on a website:
// Define a polling that fetch a user's messages by GETting a resource
var polling = AsyncPolling(function(userId, end) {
var userMessagesUrl = 'http://example.com/messages/user/' + encodeURIComponent(userId);
fetch(userMessagesUrl).then(function (response) {
var messages = // extract messages from response
end(null, messages);
}).catch(end);
}, 2000);
// Attach a listener to display the messages when they are received:
polling.on('result', function (messages) {
// Do something with the messages
});
// Run the polling for a user with ID `1`:
polling.run(1);
// Let's say they disconnect and another user log in:
polling.stop()
polling.run(newUserId);
I find this cleaner than relying on a global state or something like that.