我有2個js文件。我正在嘗試從 中的函數調用承諾的結果。如果我控制臺.log里面,它就會用數據記錄預期的對象,一切都很好,但是一旦我嘗試將值s存儲在變量中,然后控制臺此變量,它表明它內部有一些狀態(tài)平移的承諾...什么?有什么我不知道的嗎?我再次將其包裝成以獲得結果,這就是我在該函數中期望的,但是一旦我從func返回值,它就會失去承諾。問題:如何正確地將值返回給變量?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(); })}
承諾的返回值是不同的,當我在功能之外使用它時,不能真正理解為什么
翻翻過去那場雪
2022-08-27 15:24:41