node-fetch only returning promise pending

前端 未结 3 1944
终归单人心
终归单人心 2021-02-13 11:04

I am trying out node-fetch and the only result I am getting is:

Promise { }

How can I fix this so I get a completed

3条回答
  •  伪装坚强ぢ
    2021-02-13 11:34

    The problem with your code is that u.json() returns a promise

    You need to wait for that new Promise to resolve also:

    var nf = require('node-fetch');
    
    var url = 'https://api.github.com/emojis'
    
    nf(url).then(
      function(u){ return u.json();}
    ).then(
      function(json){
        console.log(json);
      }
    )
    

    For real code you should also add a .catch or try/catch and some 404/500 error handling since fetch always succeeds unless a network error occurs. Status codes 404 and 500 still resolve with success

提交回复
热议问题