Home > Net >  Nodejs async functions not running in order
Nodejs async functions not running in order

Time:09-22

I am trying to send a POST request to my backend, this POST request requires multipart/form-data, so I am converting the image locations into Buffers before I send it all as a POST request, But due to Nodejs async behaviors, I don't know how to make the functions run in order. How should I rearrange/rewrite this code?

import axios from "axios";
import { readFileSync, readFile as rf, PathOrFileDescriptor } from "fs";
import FormData from "form-data";
import dotenv from "dotenv";

dotenv.config();

// readfile promise
const readFile = (file: PathOrFileDescriptor) => {
  return new Promise((resolve, reject) => {
    rf(file, (err, data) => {
      if (err) reject(err.message);
      resolve(data);
    });
  });
};

console.log("Parsing JSON data            
  • Related