This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
@Injectable() | |
export class AppService { | |
googleLogin(req) { | |
if (!req.user) { | |
return 'No user from google' | |
} | |
return { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { |