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 };
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
import { config } from 'dotenv';
import { Injectable } from '@nestjs/common';
config();
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
const express = require('express');
const bodyParser = require('body-parser');
const sendSms = require('./twilio');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { AuthGuard } from '@nestjs/passport';
@Controller('google')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
@UseGuards(AuthGuard('google'))
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GoogleStrategy } from './google.strategy'
@Module({
imports: [],
controllers: [AppController],
providers: [AppService, GoogleStrategy],
})