Friday 14 April 2017

How do I use an asynchronous value in a function that needs to return synchronously [duplicate]

This question already has an answer here:

I feel that I'm overlooking something here, but I am trying to write a function that returns a synchronous value, but internally it needs some data that can only be fetched async:

// Gets something asynchronously
const getValueAsync = Promise.resolve('value')

// Needs to return a value synchronously
const returnSyncValue = () => {
    // Needs the async data here
    getValueAsync  

  // Modify it


  // Return the result sync
}

// So that this will work:
const value = returnSyncValue()

I'm using the latest version of node. Is there any way of achieving this without changing returnSyncValue's implementation (i.e. making it async as well)?



via ismay

No comments:

Post a Comment