为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村225694 2021-06-22 21:08:42
我的函数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                         }
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信