Ccmmutty logo
Commutty IT
1 min read

axiosつかってみた

https://cdn.magicode.io/media/notebox/flow-g33ac9f312_1280.jpg

なにがどうよさそうなのか?

axiosはpromiseベースのHTTPクライアントとのこと。
今まではこのように自分で書いていました。
wget = (url)-> new Promise (f,re)->
  request
    method: "POST"
    url: url
    encoding: null
    form:
      q: ["Hello world", "My name is Scott"]
      target: "tiger"
  , (e,r,b)->
    if e?
      re e
    else
      res = {}
      res.headers = r.headers
      res.body = cheerio.load b
      f res
axiosを利用すると下記のように短くすることができました。
require("axios").get "http://google.com"
.then (r)-> puts r
.catch (e)-> puts e

Discussion

コメントにはログインが必要です。