Node: fs.truncate after promisify do not resolve or reject Promise, if there is fs.open promisifyed function in same file

Created on 11 Apr 2018  路  3Comments  路  Source: nodejs/node

  • Version: v9.3.0
  • Platform: macOS 10.13.1 High Sierra

    I'm facing a problem when I have many fs promisifyed functions in one file, specifically promisifyed fs.truncate and fs.open in one file

This work fine, result undefined as expected

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');

fs.truncate = promisify(fs.truncate);

fs.truncate(path.resolve(__dirname, '../.logs/123.log'))
  .then(console.log)
  .catch(console.error);

fs.open = promisify(fs.open);

But if I put fs.open = promisify(fs.open) above invoking fs.truncate, my promise do not resolve or reject

fs.truncate = promisify(fs.truncate);
fs.open = promisify(fs.open);

fs.truncate(path.resolve(__dirname, '../.logs/123.log'))
  .then(console.log)
  .catch(console.error);
fs promises

Most helpful comment

Starting in node.js 10.0.0, there are experimental fs.promises functions that are not promisify wrappers... try fs.promises.truncate for instance...

All 3 comments

If I get this right, you do not need to rewrite fs methods, it can break fs module. Try this:

const truncate = promisify(fs.truncate);
const open = promisify(fs.open);

@vsemozhetbyt I think you're right. 袛褟泻褍褞 ;)

Starting in node.js 10.0.0, there are experimental fs.promises functions that are not promisify wrappers... try fs.promises.truncate for instance...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  3Comments

dfahlander picture dfahlander  路  3Comments

jmichae3 picture jmichae3  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

danialkhansari picture danialkhansari  路  3Comments