我的函數(shù)residents()從API返回一個數(shù)組,函數(shù)getTatooineResidents()應(yīng)該創(chuàng)建一個新的promise,當(dāng)promise被解決時執(zhí)行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!') })}當(dāng)我調(diào)用 getTatooineResidents() 時,它不是執(zhí)行函數(shù)residents(),而是返回函數(shù)的主體,當(dāng)我解析promise 時,括號不應(yīng)該調(diào)用函數(shù)嗎?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 }
當(dāng)我解決一個承諾時,我的函數(shù)沒有被調(diào)用
慕村225694
2021-06-22 21:08:42