我有2個(gè)js文件。我正在嘗試從 中的函數(shù)調(diào)用承諾的結(jié)果。如果我控制臺(tái).log里面,它就會(huì)用數(shù)據(jù)記錄預(yù)期的對(duì)象,一切都很好,但是一旦我嘗試將值s存儲(chǔ)在變量中,然后控制臺(tái)此變量,它表明它內(nèi)部有一些狀態(tài)平移的承諾...什么?有什么我不知道的嗎?我再次將其包裝成以獲得結(jié)果,這就是我在該函數(shù)中期望的,但是一旦我從func返回值,它就會(huì)失去承諾。問(wèn)題:如何正確地將值返回給變量?index.jsall()listings.jsasync function all()returnindex.jawait索引.jsimport 'babel-polyfill';import {all} from './listings';let result = all();console.log(result); //returns promise, why???列表.jsexport async function all() { let res = await makeRequest('http://sellbuyschool42.com/listings'); console.log(res); //return expected object with data, all is GOOD return res;}function makeRequest($url, options= {}){ return fetch($url).then((responce) => { if(responce.status != 200) { return responce.text().then((text) => { throw new Error(text); }) } return responce.json(); })}
承諾的返回值是不同的,當(dāng)我在功能之外使用它時(shí),不能真正理解為什么
翻翻過(guò)去那場(chǎng)雪
2022-08-27 15:24:41