JavaScript: Executing synchronously

I am being ask this question often; therefore, I am posting the answer here.

Commonly there are those who wish to do a process that normally is asynchronously, such as performing AJAX operations in a synchronous fashion. There are a few ways to do this.

If you are coding in JS for NodeJS, then you can use the package async-waterfall, example:

  function readFile(callback) {
        fs.readFile('your_file.json', function (error, file) {
            if (error) {
            } else {
                callback(null, file);
    function processFile(file, callback){
        // Some code here
    ], function (error) {
        if (error) {
            // handle error made in readFile or processFile method 

Else, you can use promise chains which is part of Javascript, example:

    new Promise(function(resolve, reject) {
          reject("error message");
    }).then(function(result) {
      return processFile(result)    

Or, you can use async/await:


Express Gateway: Tips

Configuration: Order of Policies Matter

When working on Auth0, make sure that the order in which you set the pipelines’ policies matches the order in which the policies are listed:
Order of policies matter

Metrics: Use epimetheus or prom-client Package

You can use either prom-client or epimetheus.
Personally, I found easier to use the epimetheus package in Gateway Express:

'use strict';

const epimetheus = require('epimetheus');

module.exports = function (gatewayExpressApp) {
  epimetheus.instrument(gatewayExpressApp, {url: '/metrics'});

Something to considered is that some of the metrics display by epimetheus and prom-client differ.
For example, with epimetheus the http request duration are in milliseconds:

http_request_duration_milliseconds{quantile="0.01",method="get",path="/api/",cardinality="one",status="200"} 1

while in prom-client, the http request duration are in seconds:

http_request_duration_seconds_bucket{service="api",instance="api-3432dc43c4-h3eeg",le="0.005",path="/metrics",method="GET",apiVersion="1.0.0",status="200"} 466081


Express Gateway use express. I tried different swagger packages; however, the closest results was to display all the endpoints from the admin side of Express Gateway.
Then, I decided to check the code implemented in the restify-swagger-jsdoc and it did the trick. Here is the modified code:

'use strict';

const swaggerJSDoc = require("swagger-jsdoc");
const path = require("path");
const fs = require("fs");
const mime = require("mime-types");
const swaggerUiPath = path.dirname(require.resolve('swagger-ui'));
const publicPath = '/api'.replace(/\/+$/, '');

const swaggerSpec = swaggerJSDoc({
	swaggerDefinition: {
		info: {
			title: 'API documentation',
			version: '0.0.1',
			description: 'API documentation'
		host: undefined,
		basePath: '/',
		schemes: undefined,
		tags: []
	apis: [

module.exports = function (gatewayExpressApp) {

	gatewayExpressApp.get(`${publicPath}/swagger.json`, (req, res) => {
		res.setHeader('Content-type', 'application/json');

	gatewayExpressApp.get(new RegExp(publicPath + '\/?$'), (req, res) => {
		res.setHeader('Location', `${publicPath}/index.html`);

	gatewayExpressApp.get(new RegExp(publicPath + '\/(.*)$'), (req, res) => {
		fs.readFile(path.resolve(swaggerUiPath, req.params[0]), (err, content) => {
			if (err) {
				res.send(`File ${req.params[0]} does not exist`);
			if (req.params[0] === 'index.html') {
				let jsonFileUrl =  `${ ? 'https' : 'http'}`.concat('://').concat(`${}${publicPath}/swagger.json`); 
				content = new Buffer(content.toString().replace('url = ""', `url = "${jsonFileUrl}"`)); 
			const contentType = mime.lookup(req.params[0]); 
			if (contentType !== false) { 
				res.setHeader('Content-Type', contentType);