ちょっとスクレイピングする機会があり、Nightmareでスクリプトを書いたんだけど、Promiseをベタベタ書いたりする必要があって面倒だった。
Node.js v7.6.0からはasync/awaitが使えるようになったけど、スクリプト全体をasync functionで囲んだり .catch()
する必要があったりして、ノイズが多い。
というわけで、トップレベルにいきなり await
を書いても実行できる async-node
というコマンドを作った。
インストール
npm i -g @fand/async-node
使用例1) GIPHYから画像をダウンロード
Nightmareで画像を検索し、kevva/downloadで手元にダウンロードするスクリプト。
ソース: https://github.com/fand/async-node/tree/master/examples/image-downloader
使用例2) MongoDBのデータを取得/挿入
$ async-node mongo.js -s '{"name":"foo"}' # 挿入 $ async-node mongo.js -g '{"name":"foo"}' # 取得
ソース: https://github.com/fand/async-node/tree/master/examples/mongo
どうぞご利用ください🔪🔪🔪