Skip to content

Instantly share code, notes, and snippets.

@arisetyo
Created February 28, 2021 08:11
Show Gist options
  • Save arisetyo/7d20d0c5604ec3c8260ecf88e27acaf7 to your computer and use it in GitHub Desktop.
Save arisetyo/7d20d0c5604ec3c8260ecf88e27acaf7 to your computer and use it in GitHub Desktop.
Introduction to async/await in Node JS
/**
* @author Arie M. Prasetyo
*/
const mongoose = require('mongoose');
const mongoConfig = {useNewUrlParser: true, useUnifiedTopology: true};
// connect to Mongo database
mongoose.connect('mongodb://localhost:27017/test', mongoConfig);
// define the model
const Cat = mongoose.model('Cat', {name: String});
// = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/**
* This is a normal async javascript function
* As you can see here, the log in line 35 was executed without waiting
* for the MongoDB query (line 31) to be finished.
* This the nature of JavaScript's asynchronous behaviour.
*/
const runQuery = () => {
Cat.find().then(value => {
console.log(value)
});
console.log('run the following line\n');
}
// Run test
runQuery();
/**
* @author Arie M. Prasetyo
*/
const mongoose = require('mongoose');
const mongoConfig = {useNewUrlParser: true, useUnifiedTopology: true};
// connect to Mongo database
mongoose.connect('mongodb://localhost:27017/test', mongoConfig);
// define the model
const Cat = mongoose.model('Cat', {name: String});
// = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/**
* If we want to do something synchronously, we can use the `async` and `await` commands.
* Take a look at the example below:
*
* This function uses async and await.
* await is a command that will make the line to be completed before moving to the next line.
*/
const runQuery = async () => {
const p = Cat.find().exec();
await p.then(value => {
console.log(value)
});
console.log('run the following line\n');
}
// Run test
runQuery();
/**
* @author Arie M. Prasetyo
*/
const {Sequelize, DataTypes} = require('sequelize');
// connect to MySQL database
const sequelize = new Sequelize('database_for_tests', 'root', process.env.MYSQL_PWD, {
host: 'localhost',
dialect: 'mysql',
logging: false
});
// define the model
const Cat = sequelize.define('cat', {
name: {
type: DataTypes.STRING
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'cat'
});
// = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/**
* Run the query in a default asynchronous manner
*/
const runQuery = () => {
Cat
.findAll({raw: true})
.then(value => {
console.log(value);
});
console.log('run the following line\n'); // This line will be executed before the query in line 30 is completed
}
// Run test
runQuery();
/**
* @author Arie M. Prasetyo
*/
const {Sequelize, DataTypes} = require('sequelize');
// connect to MySQL database
const sequelize = new Sequelize('database_for_tests', 'root', process.env.MYSQL_PWD, {
host: 'localhost',
dialect: 'mysql',
logging: false
});
// define the model
const Cat = sequelize.define('cat', {
name: {
type: DataTypes.STRING
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'cat'
});
// = = = = = = = = = = = = = = = = = = = = = = = = = = = =
/**
* Run the query in a "synchronous" manner using async await.
* There are several variations of how we can access the data
* returned by the database query
*/
const runQuery = async () => {
// Variation A
/*
let p = Cat.findAll({raw: true}) // get only the data
await p.then(value => {
console.log(value);
});
*/
// Variation B
/*
await Cat
.findAll({raw: true}) // get only the data
.then(value => {
console.log(value);
});
*/
// Variation C
let cat = await Cat
.findAll({raw: true}) // get only the data
.then(value => {
return value;
});
console.log(cat);
console.log('run the following line\n');
}
// Run test
runQuery();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment