Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Typescript Async/ await with promise in Node JS FS Module

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
    };
like image 459
Shan Khan Avatar asked Feb 27 '16 18:02

Shan Khan


1 Answers

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')
like image 176
Felipe Plets Avatar answered Oct 11 '22 18:10

Felipe Plets