Skip to content

fs.createWriteStream: add util.promisify support. #16989

Closed
@coreyfarrell

Description

@coreyfarrell
  • Version: v8.9.0
  • Platform: Linux lt1.cfware.com 4.13.10-200.fc26.x86_64 deps: update openssl to 1.0.1j #1 SMP Fri Oct 27 15:34:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: fs
#!/usr/bin/env node
'use strict';

const util = require('util');
const fs = require('fs');
const createWriteStream = util.promisify(fs.createWriteStream);
const {spawn} = require('child_process');

async function main() {
  const log = await createWriteStream('output.log');
  spawn('ls', ['-lh'], { stdio: ['ignore', log, log]});
  return 'Done';
}

main().then(console.log).catch(console.error);

This causes 'output.log' to be created but empty, node.js exits silently.

I've created a "polyfill" which seems to work and do what I would expect:

const fs = require('fs');
const util = require('util');
fs.createWriteStream[util.promisify.custom] = (path, options) => {
  const stream = fs.createWriteStream(path, options);
  return new Promise((resolve, reject) => {
    stream.on('error', reject);
    stream.on('open', resolve);
  });
};

I'm not yet prepared to submit a pull request for this (it will be my first to node). I have code in lib/fs.js that I think correctly uses the internal version of the promises, I haven't had a chance to build. I'm not sure what automatic tests would be needed for this feature, I've only tested this manually. I'm reading the contributor guide but still wanted to submit the bug report now.

I also haven't had a chance to check out other fs API's like createReadStream, probably appropriate to have similar promisify support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.fsIssues and PRs related to the fs subsystem / file system.promisesIssues and PRs related to ECMAScript promises.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions