'use strict;'の状態でyieldはトップレベルで使えない
きっかけ
がバージョン上がって、これまでのPhantomJSベース→Electronベースになったとのことで試してみました。
一番最初に載っているサンプルコードを試してみた
'use strict'; var Nightmare = require('nightmare'); yield Nightmare() .goto('http://yahoo.com') .type('input[title="Search"]', 'github nightmare') .click('.searchsubmit');
を実行すると・・・
yield Nightmare()
^^^^^
SyntaxError: Unexpected strict mode reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
エラーが。。
やっぱ使えないみたい
によれば、
You can't. yield can only be used within a generator function.
だそう。
'use strict'; const Nightmare = require('nightmare'); const co = require('co'); co(function *() { const nightmare = Nightmare(); const title = yield nightmare .goto('http://cnn.com') .evaluate(function() { return document.title; }); console.log(title); yield nightmare.end(); });
co使えばいけました。
ちなみに
最初のコードはcoを使っても動きませんでした。なんでだろ。。