How to use Typescript async / await function and return typescript default promises in node js FS module and call other function upon promise resolved.
Following is the code :
  if (value) {
     tempValue = value;
     fs.writeFile(FILE_TOKEN, value, WriteTokenFileResult);
            }
 function WriteTokenFileResult(err: any, data: any) {
        if (err) {
            console.log(err);
            return false;
        }
        TOKEN = tempValue;
        ReadGist(); // other FS read File call
    };
                Since NodeJS 10.0.0 fsPromises module can be used to achieve this result.
import { promises as fsPromises } from 'fs';
await fsPromises.writeFile('file.txt', 'data')
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With