Skip to content

Instantly share code, notes, and snippets.

View chetanraj's full-sized avatar
🎯
Focusing

Chetan chetanraj

🎯
Focusing
View GitHub Profile
@chetanraj
chetanraj / package.js
Last active September 5, 2022 08:34
nifty-npm-tips
{
"name": "nifty-npm-tips",
"version": "0.0.1",
"private": true,
"dependencies": { },
"devDependencies": { },
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
// get books
const getBookByName = (name) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ name: 'Art of War', author: 'Sun Tsu', ISBN: 9780140439199 });
}, 500);
});
};
// check if that book exists
@chetanraj
chetanraj / beingAsync.js
Created August 15, 2021 12:42
beingAsync
const beingAsync = async () => {
const book = await getBookByName('Art of War');
const { stock } = await checkIfBookExists(book.ISBN);
if (stock > 0) {
const response = getTheBookPrice(book.ISBN);
console.log(`Book price is $${response.offers.booksrun.new.price}!`);
} else {
console.log('Book not found!');
}
};
@chetanraj
chetanraj / getBookByNamePromise.js
Created August 15, 2021 12:41
getBookByNamePromise
getBookByName('Art of War').then((book) => {
if (book) {
checkIfBookExists(book.ISBN).then((data) => {
if (data.stock > 0) {
getTheBookPrice(data.ISBN).then((response) => {
console.log(response.offers.booksrun.new.price); // uff 😅
});
}
});
} else {
@chetanraj
chetanraj / getTheBookPrice.js
Last active August 15, 2021 12:39
getTheBookPrice
// hit BooksRun API - To get the new, used and rent price
getTheBookPrice = (ISBN) => {
return new Promise((resolve, reject) => {
const URL = `https://booksrun.com/API/v3/price/buy/${ISBN}?key=${process.env.API_KEY}`;
fetch(URL)
.then((response) => {
resolve(response.json());
})
.catch((err) => {
@chetanraj
chetanraj / getBookByNamePromise.js
Created August 15, 2021 12:37
getBookByNamePromise
getBookByName('Art of War').then((book) => {
if (book) {
checkIfBookExists(book.ISBN).then((data) => {
console.log(data.stock > 0);
});
} else {
console.log('Book not found!');
}
});
@chetanraj
chetanraj / checkIfBookExists.js
Created August 15, 2021 12:34
checkIfBookExists
// check if that book exists
checkIfBookExists = (ISBN) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ stock: 10, ISBN: 9780140439199 });
}, 500);
});
};
@chetanraj
chetanraj / getBookByNamePromise.js
Created August 14, 2021 15:05
getBookByNamePromise
getBookByName('Art of War').then((book) => {
if (book) {
console.log('Found the book!');
} else {
console.log('Book not found!');
}
});
@chetanraj
chetanraj / getBookByName.js
Created August 14, 2021 15:03
getBookByName
// get books
const getBookByName = (name) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ name: 'Art of War', author: 'Sun Tsu', ISBN: 9780140439199 });
}, 500);
});
};
@chetanraj
chetanraj / useThemeExample.js
Created August 10, 2021 15:17
useThemeExample
function useTheme(theme) {
useLayoutEffect(
() => {
// Iterate through each value in theme object
for (const key in theme) {
// Update css variables in document's root element
document.documentElement.style.setProperty(`--${key}`, theme[key]);
}
},
[theme] // Only call again if theme object reference changes