Skip to content

Instantly share code, notes, and snippets.

View OlivierJM's full-sized avatar

ojm OlivierJM

View GitHub Profile
const name = "Johnaas"
const age = 35;
const city = "lusaka";
const message = `my name is ${name} am ${age} years old and I live in ${city}`
console.log(message); // "my name is Johnaas am 35 years old and I live in lusaka"
function calculateAge(bornYear, currentYear){
console.log(currentYear - bornYear);
}
calculateAge(2000, 2018); // 18
calculateAge() // NAN
// cheap fix
function calculateAge(bornYear, currentYear){
if(bornYear !== undefined && currentYear !== undefined){
console.log(currentYear - bornYear);
} else {
console.log("provide both year values")
}
}
calculateAge(2000, 2018); // 18
function calculateExactAge(bornYear=1995, currentYear=2018){
console.log(currentYear - bornYear);
}
calculateExactAge(); // 23
calculateExactAge(2010, 2020); // 10
function getData() {
HTTP.call('GET', 'https://jsonplaceholder.typicode.com/posts');
console.log('Going to grab data');
}
function getData() {
HTTP.call('GET', 'https://jsonplaceholder.typicode.com/posts', (err, res) => {
if(err){
console.log(err);
} else {
console.log(res);
}
});
console.log('Going to grab data');
}
@OlivierJM
OlivierJM / asyncfetchData.js
Last active August 7, 2018 15:04
use the async and await with axios on multiple request with multiple promises
getRemoteData = async () => {
try {
const coursePromise = axios('http://x.x.x.x/api/course/');
const topicPromise = axios('http://x.x.x.x/api/topic/');
const [ courses, topics ] = await Promise.all(coursePromise, topicPromise]);
this.setState({
courses: courses.data.data,
topics: topics.data.data,
loading: false,
});
// functional component with partial destructuring
const UserSection = ({ user, isActive }) => (
<div>
<p>{user.name}</p> <span> {isActive ? 'active' : 'offline'} </span>
</div>
);
// functional component with no destructuring
const UserSection = props => (
// class component with destructuring
class UserSection extends React.Component {
render(){
const { user, isActive } = this.props;
return(
<div>
<p>{user.name}</p> <span>{isActive ? 'active' : 'offline'}</span>
</div>
)
}

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

Pull Request Process

  1. Ensure that the project is building successfully