0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-fs-promisified.js
Dan Fabulich 65531d0e86 fs: promisify exists correctly
PR-URL: https://github.com/nodejs/node/pull/13316
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-06-01 15:19:29 -07:00

39 lines
1020 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
common.crashOnUnhandledRejection();
const read = promisify(fs.read);
const write = promisify(fs.write);
const exists = promisify(fs.exists);
{
const fd = fs.openSync(__filename, 'r');
read(fd, Buffer.alloc(1024), 0, 1024, null).then(common.mustCall((obj) => {
assert.strictEqual(typeof obj.bytesRead, 'number');
assert(obj.buffer instanceof Buffer);
fs.closeSync(fd);
}));
}
common.refreshTmpDir();
{
const filename = path.join(common.tmpDir, 'write-promise.txt');
const fd = fs.openSync(filename, 'w');
write(fd, Buffer.from('foobar')).then(common.mustCall((obj) => {
assert.strictEqual(typeof obj.bytesWritten, 'number');
assert.strictEqual(obj.buffer.toString(), 'foobar');
fs.closeSync(fd);
}));
}
{
exists(__filename).then(common.mustCall((x) => {
assert.strictEqual(x, true);
}));
}