Skip to content

Instantly share code, notes, and snippets.

@Nek

Nek/index.js

Created Feb 5, 2014
Embed
What would you like to do?
working code
function *listen(el, evType) {
while (true)
yield function(cb) {
var fire = function(ev) {
el.removeEventListener(evType, fire);
cb(null, ev);
}
el.addEventListener(evType, fire);
}
}
var gens = require('gens');
function go(gen) {
gens(gen)(function(err) { if (err) throw err; });
}
var jsonp = require('jsonp');
function fetch(url) {
return jsonp.bind(null, url);
}
function render(el, items) {
var items = items.map(function(item) {
return '<li>' + item + '</li>';
});
el.innerHTML = '<ul>' + items.join(''); + '</ul>';
}
go(function*() {
var results = document.getElementById('results');
var query = document.getElementById('query');
var clicks = listen(document.getElementById('search'), 'click');
while (true) {
yield clicks.next().value; // wait for 'click' event
var url = 'http://en.wikipedia.org/w/api.php' +
'?action=opensearch&format=json&search=' +
encodeURIComponent(query.value);
var data = yield fetch(url);
render(results, data[1]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.