LoginSignup
13
5

More than 5 years have passed since last update.

Node.js10のfs/promisesきたよー

Last updated at Posted at 2018-04-27

Node.jsのcurrentにversion10がきた

Promiseでかけるfsが来た。Experimentalなので注意。

試す

Node.jsのcurrentでversion10が来たのでfs/promisesを試してみる。

nodebrewで入れてるので更新

nodebrew install-binary 10
nodebrew use 10
const fs = require('fs/promises')

で引っ張ってくるとPromiseバージョンのAPIを使える。
これ の最後のと同じことをする

コード

上記でやっている、
ファイル1を取ってきてその中に書いてあるパスのファイル2の内容を出力
をやってみる。

const fs = require('fs/promises');

(async () => {
  const nextFile = await fs.readFile('./next.txt', 'utf8');
  const message = await fs.readFile(nextFile.trim(), 'utf8');

  console.log('async/await:', message);
})()
.catch((err) => console.log(err.message));

使い方の違いはない。
Util等でpromisifyするのと比べて関数を引っ張ってくるのがわかりやすい。

実行結果

% node index.js
(node:5106) ExperimentalWarning: The fs/promises API is experimental
async/await: Hello Node10

ExperimentalWarning って出てる。

warnなし

% node --no-warnings index.js
async/await: Hello Node10

はやく安定版にしてけれ

13
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
5