javascript - How do I use an asynchronous value in a function that needs to return synchronously -
this question has answer here:
- how return response asynchronous call? 21 answers
i feel i'm overlooking here, trying write function returns synchronous value, internally needs data can fetched async:
// gets asynchronously const getvalueasync = promise.resolve('value') // needs return value synchronously const returnsyncvalue = () => { // needs async data here getvalueasync // modify // return result sync } // work: const value = returnsyncvalue()
i'm using latest version of node. there way of achieving without changing returnsyncvalue's implementation (i.e. making async well)?
short answer: no
long answer:
you can't because when const value = returnsyncvalue()
, haven't gotten async value yet. therefore there no value assign.
Comments
Post a Comment