Proper request with async/await in Node.JS

前端 未结 4 1411
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 12:54

In my program I make async call for my function from another API module:

var info = await api.MyRequest(value);

Module

4条回答
  •  星月不相逢
    2020-12-05 13:35

    Since request-promise has been deprecated, here are other options that don't depend on the NPM request package. got has been mentioned already, but it depends on 11 other packages. axios, in contrast, only has 1 dependency (for redirects). Everything else is natively implemented and built on top of the native NodeJS packages.

    Here is the same example using axios:

    const axios = require('axios')
    
    const response = await axios.get(url)
    const result = response.data
    

    One-liner in TypeScript:

    const {data} = await axios.get(url)
    

提交回复
热议问题