reactjs - Returning the value of a promise to another function -
this question has answer here:
- how return response asynchronous call? 21 answers
i'm trying promisevalue
in renderselect. (in tmp
)
the thing i'm getting promise, normal behavior. don't understand how values inside promise can work them.
i have apiservice
class follows
handleresponse(response) { if (response.status >= 200 && response.status < 300) { return response.json(); } return response.json() .then((res) => { throw new error(res.message); }, () => { throw new error(); }); } get(url) { return fetch(`${this.api_url}${url}`, { method: 'get', headers: this.headers, }) .then(response => this.handleresponse(response)); }
and .jsx
this
const getoptions = (contractor_employee_id) => { const apiservice = new apiservice() return apiservice .get(`some_url`) .then( response => { return ["toto", "tata", "titi"] }, err => (console.log("failed")) ); }; const renderselect = (field) => { const tmp = getoptions(field.id) console.log(tmp)
promises asynchronous. , if want return promise result in synchronous way, use async / await.
const renderselect = async (field) => { const tmp = await getoptions(field.id) console.log(tmp) }
Comments
Post a Comment