Skip to content

Instantly share code, notes, and snippets.

View rbren's full-sized avatar
🐢
I may be slow to respond.

Robert Brennan rbren

🐢
I may be slow to respond.
  • Fairwinds
  • Boston, Massachusetts
View GitHub Profile
@rbren
rbren / async.js
Last active October 11, 2017 15:47
let hn = require('@datafire/hacker_news').create();
(async () => {
let storyIDs = await hn.getStories({storyType: 'top'});
let topStory = await hn.getItem({itemID: storyIDs[0]});
console.log(`Top story: ${topStory.title} - ${topStory.url}`);
})();
@rbren
rbren / promise.js
Last active September 29, 2017 17:57
let hn = require('@datafire/hacker_news').create();
Promise.resolve()
.then(_ => hn.getStories({storyType: 'top'}))
.then(storyIDs => hn.getItem({itemID: storyIDs[0]))
.then(topStory => console.log(`Top story: ${topStory.title} - ${topStory.url}`))
@rbren
rbren / callback.js
Last active September 29, 2017 17:56
// Note: this code doesn't work!
let hn = require('@datafire/hacker_news').create();
hn.getStories({storyType: 'top'}, (err, storyIDs) => {
if (err) throw err;
hn.getItem({itemID: storyIDs[0]}, (err, topStory) => {
if (err) throw err;
console.log(`Top story: ${topStory.title} - ${topStory.url}`);
})
})
@rbren
rbren / sync.js
Last active September 29, 2017 17:51
// Note: this code doesn't work!
let hn = require('@datafire/hacker_news').create();
let storyIDs = hn.getStories({storyType: 'top'});
let topStory = hn.getItem({itemID: storyIDs[0]});
console.log(`Top story: ${topStory.title} - ${topStory.url}`);
function getThing(callback) {
api.getItem(1)
.then(item => setTimeout(_ => callback(null, item)))
.catch(e => setTimeout(_ => callback(e)));
}
getThing(function(err, thing) {
if (err) throw err;
console.log(thing);
})
function getThing(callback) {
api.getItem(1)
.then(item => callback(null, item))
.catch(e => callback(e));
}
getThing(function(err, thing) {
if (err) throw err;
console.log(thing);
})
api.getItem(1)
.then(item => {
delete item.owner;
console.log(item.owner.name);
})
.catch(e => {
console.log(e); // Cannot read property 'name' of undefined
})
api.getItem(1, (err, data) => {
if (err) throw err;
item.amount++;
api.updateItem(1, item, (err, update) => {
if (err) throw err;
api.deleteItem(1, (err) => {
if (err) throw err;
})
})
})
Promise.resolve()
.then(_ => {
return api.getItem(1)
})
.then(item => {
item.amount++
return api.updateItem(1, item);
})
.then(update => {
return api.deleteItem(1);
fs.readFile('index.html', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
})