https://github.com/steemit/steem-js
Intermediate
In this tutorial I will show you how to get details of posts you upvoted using the steem.js API. Instead of callbacks I will use promises.
To retrieve our votes and the corresponding posts we need the following API functions:
steem.api.getAccountVotes
steem.api.getContent
These two functions using callbacks to handle asynchrony. As we dont want to end in the "callback-hell" we are going to use promises:
function getAccountVotes(author) {
return new Promise((resolve, reject) => {
// get all votes for specific author
steem.api.getAccountVotes(author, (error, result) => {
// reject if error
if (error) {
reject(error);
// return votes if success
} else {
resolve(result);
}
});
});
}
function getContent(author, permlink) {
return new Promise((resolve, reject) => {
// get all details for specific authors post
steem.api.getContent(author, permlink, (error, result) => {
if (error) {
// reject if error
reject(error);
} else {
// return post details if success
resolve(result);
}
});
});
}
The previous code retrieves the last ten votes and creates an array of content requests to retrieve all details.
// get all votes for myself
getAccountVotes('drookyn')
.then((votes) => {
// get last 10 votes
const lastTenVotes = votes.reverse().slice(0, 10);
// return new promise with all detail promises
return Promise.all(lastTenVotes.map((vote) => {
const authorPerm = vote.authorperm.split('/');
return getContent(authorPerm[0], authorPerm[1]);
}));
})
.then(posts => console.log(posts.map(p => p.title))) // log all titles
.catch(err => console.log(err));
We just printed the title
of each post in the posts
array above. But what else do these objects contain?
All the code can be found in this paste.