asyncを書くのも面倒なのでasync-nodeを作った

https://cloud.githubusercontent.com/assets/1403842/24457202/7dca1bce-14d0-11e7-9df7-d1a674b46198.png

ちょっとスクレイピングする機会があり、Nightmareでスクリプトを書いたんだけど、Promiseをベタベタ書いたりする必要があって面倒だった。 Node.js v7.6.0からはasync/awaitが使えるようになったけど、スクリプト全体をasync functionで囲んだり .catch() する必要があったりして、ノイズが多い。

というわけで、トップレベルにいきなり await を書いても実行できる async-node というコマンドを作った。

github.com

インストール

npm i -g @fand/async-node

使用例1) GIPHYから画像をダウンロード

giphy_opt

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

どうぞご利用ください🔪🔪🔪