Skip to content

Instantly share code, notes, and snippets.

View iMichaelOwolabi's full-sized avatar
✌️
Impossible is Nothing!

Michael Owolabi iMichaelOwolabi

✌️
Impossible is Nothing!
View GitHub Profile
@iMichaelOwolabi
iMichaelOwolabi / samplePromise.js
Last active February 9, 2022 11:35
Example promise in Node.js
/* Sample promises to simulate asynchronous operation.
* Let's assume these are sample DB operations and their equivalent response time
*
*/
const samplePromise1 = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Promise 1 resolved after 2 seconds.');
}, 2000);
});
@iMichaelOwolabi
iMichaelOwolabi / blockingAwait.js
Last active February 8, 2022 07:40
Sample use of blocking promise implementation in Node.js
// Do not do this since the awaits are not related in any way but are blocking ⛔️
const blockingAsnycFunction = async () => {
// Start recording the execution time of the following code.
console.time('blocking-await');
const promiseResult1 = await samplePromise1();
const promiseResult2 = await samplePromise2();
console.log(promiseResult1);
console.log(promiseResult2);
@iMichaelOwolabi
iMichaelOwolabi / nonBlockingAwait.js
Last active February 8, 2022 07:40
Sample use of non-blocking promise implementation in Node.js
// Do this instead because unrelated awaits doesn't block each other ✅
const nonBlockinAsnycFunction = async () => {
// Start recording the execution time of the following code.
console.time('non-blocking-await');
const promise1 = samplePromise1();
const promise2 = samplePromise2();
// Use promise.all to ensure the two promises are executed in parallel.
const [promiseResult1, promiseResult2, promiseResult3] = await Promise.all([promise1, promise2, promise3]);
@iMichaelOwolabi
iMichaelOwolabi / dependentPromiseSample.js
Created February 7, 2022 08:28
Sample dependent promise data/record
/**
* Dependent promise example
*/
// Employee records in the DB
const employee = [
{ id: 1, firstName: 'Ayo', lastName: 'Tunmise', dob: '1980-07-28' },
{ id: 2, firstName: 'Dayo', lastName: 'Owolabi', dob: '2000-03-08' },
{ id: 3, firstName: 'Michael', lastName: 'Ikechukwu', dob: '1997-01-31' },
{ id: 4, firstName: 'Hammed', lastName: 'Tukur', dob: '1900-08-07' },
@iMichaelOwolabi
iMichaelOwolabi / blockingDependentPromise.js
Created February 7, 2022 08:30
Blocking dependent promise implementation sample
// Application logic that get employee records alongside their individual next of kin data
const getAllEmployeeInformation = async () => {
// Start recording the execution time of the following code.
console.time('bad await')
const allEmployees = await getAllEmployees(); // Get all employee from the DB
const allEmployeeRecords = [];
// Map individual employee record with their next of kin data and return the mapped data
for (const employee of allEmployees) {
@iMichaelOwolabi
iMichaelOwolabi / nonBlockingDependentPromise.js
Created February 7, 2022 08:31
Non-blocking dependent promise implementation sample
// Application logic that get employee records alongside their individual next of kin data in a non blocking format
const getAllEmployeeInformation = async () => {
// Start recording the execution time of the following code.
console.time('better await')
const allEmployees = await getAllEmployees(); // Get all employee from the DB
// Map individual employee record with their next of kin data and return the mapped data
const employeeNextOfKinPromise = allEmployees.map(employee => {
return getEmployeeNextOfKin(employee.id).then(nextOfKin => {
return { ...employee, nextOfKin };
@iMichaelOwolabi
iMichaelOwolabi / eventLoopBlockingFileREading.js
Last active February 13, 2022 20:08
Sample event loop blocking code
/**
* Event loop blocking code sample
*/
const fs = require('fs');
const countries = fs.readFileSync('countries.csv');
console.log(countries.toString());
// Other JavaScript code below this line will be blocked until the file reading completes.
console.log('Other JAVASCRIPT THAT IS BLOCKED');
@iMichaelOwolabi
iMichaelOwolabi / nonEventLoopBlockingFileReading.js
Created February 13, 2022 20:07
How to read file in Node.js in a way that it doesn't block the event loop code sample
// A better way of reading file that doesn't block the event loop
const fs = require('fs');
fs.readFile('countries.csv', (error, data) => {
if (error) {
throw new Error(error);
}
console.log(data.toString());
});
// Other JavaScript code below this line will not be blocked which is good.
@iMichaelOwolabi
iMichaelOwolabi / event-mngr-init-app.js
Created July 3, 2022 20:59
init server setup event mngr
import express from 'express';
import { config } from 'dotenv';
config();
const app = express();
app.use(express.json());
const port = process.env.PORT ?? 3000;
@iMichaelOwolabi
iMichaelOwolabi / redis-enterprise-dbConnect.js
Last active September 25, 2022 22:20
Redis enterprise connection file
import { config } from 'dotenv';
import { Client } from 'redis-om';
config();
const redisCloudConnectionString = `redis://${process.env.REDIS_DB_USER}:${process.env.REDIS_DB_PASS}@${process.env.REDIS_DB_URL}`;
const redisClient = new Client();
await redisClient.open(redisCloudConnectionString);