Upgrade to Pro — share decks privately, control downloads, hide ads and more …

eater

 eater

node 学園付属小学校の2回目で話した eater の話です。

Yosuke Furukawa

April 26, 2016
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. child_process.spawn const spawn = require('child_process').spawn; const ls = spawn('ls', [‘-al']);

    ls.stdout.on('data', (data) => { console.log(data); }); ls.stderr.on('data', (data) => { console.log(data); }); ls.on('exit', (code) => { if (code !== 0) console.error(`Error occurred`); });
  2. child_process.exec const exec = require('child_process').exec; exec(‘ls -al‘, (err, stdout, stderr)

    => { console.log(`${stdout}`); console.error(`${stderr}`); if (error !== null) { console.error(`Error occurred`); } });
  3. child_process.fork const fork = require('child_process').fork; const ls = fork(‘ls.js’); ls.stdout.on('data',

    (data) => { console.log(data); }); ls.stderr.on('data', (data) => { console.log(data); }); ls.on('exit', (code) => { if (code !== 0) console.error(`Error occurred`); });
  4. spawn > exec ͜ͷॱͰ simple exec > spawn ͜ͷॱͰ easy

    spawn > fork ͜ͷॱͰ simple fork > spawn ͜ͷॱͰ easy
  5. mockΛ։์͠ͳͯ͘OK // you don’t need to fix mock/spy console.log =>

    (message) { assert(message.match(/Foo Bar/)); }; const mockDate = new Date(2016, 3, 9); Date.now = () => { return mockDate.valueOf(); }; process.exit => (exitcode) { assert(exitcode === 1); };