猿问

当我解决一个承诺时,我的函数没有被调用

我的函数residents()从API返回一个数组,函数getTatooineResidents()应该创建一个新的promise,当promise被解决时执行residents()


const superagent = require('superagent')

const residents=()=>{

    superagent.get('https://swapi.co/api/planets/1/')

                        .then(res => 

console.log(res.body.residents))

                        .catch(err => console.log(err))

    return residents                         

}

residents()


const getTatooineResidents=()=>{

    return new Promise((resolve, reject) => {

        if(3 > 2) resolve(residents())//instead of executing residents() it just writes the body of the function

        reject('I am broken!')

      })

}

当我调用 getTatooineResidents() 时,它不是执行函数residents(),而是返回函数的主体,当我解析promise 时,括号不应该调用函数吗?


AssertionError [ERR_ASSERTION]: 

          The function should a return promise which resolves with an array of urls for the residents of Tatooine like : 

        [array of URLS] 

          the promise that was returned from you function resolved with: ()=>{

    superagent.get('https://swapi.co/api/planets/1/')

                        .then(res => console.log(res.body.residents))

                        .catch(err => console.log(err))

return residents                         

}


慕村225694
浏览 132回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答