Mantzas
Mantzas

Reputation: 2653

promise and blocking function that returns a value

I am trying to return a value from a function that calls a third lib which uses promises.

let's say i have a function (typescript):

static get(): string {
   return ??? lib.test().done(()=> {return "done"; }).fail(()=>{ return "fail"});
}

Inside I am calling a 3rd party library that returns a promise. How can I return from this function only the value that the lib returns as a promise without rewriting my function? I know this defeats the purpose of promises but the lib does only provide promises as return values.

Upvotes: 1

Views: 1545

Answers (1)

jfriend00
jfriend00

Reputation: 707456

A promise represents an asynchronous result that will be available some time in the future. You cannot make an asynchronous result into a synchronous result in Javascript. It just cannot be done. Your operation will need to be treated as asynchronous by the caller and the interface to your method will need to be asynchronous (probably using the promise or you could hide the promise and go back to a plain callback if you wanted).

Upvotes: 2

Related Questions