0

I have a Promise-based Nodejs-function that I want to call from a normal function (not an async function).

Therefore, I would like to create a wrapper function that is synchronous (returning T instead of returning Promise<T>).

How would you solve this with Nodejs?

Mike76
  • 899
  • 1
  • 9
  • 31
  • Rewrite the function so it isn't promise based in the first place (which is likely to be impossible). Asynchronous code is usually asynchronous for good reasons. – Quentin Oct 20 '20 at 15:51
  • You can write a function that returns `T` instead of `Promise` but how will you call this function? You can't call it synchronously because it won't immediately return the required value. – Yousaf Oct 20 '20 at 15:51
  • I can call it synchronously because this is about a command-line task that would need to block anyways, regardless of whether I am using Promises or not. – Mike76 Oct 20 '20 at 15:54
  • Rewriting the promise-based function is not so easy because it is within a third-party-library. – Mike76 Oct 20 '20 at 15:55
  • Then don't use promises, just write normal blocking code. You can't make promise-based code to be blocking. It will be asynchronous no matter what you do. – Yousaf Oct 20 '20 at 15:55
  • As mentioned, the asynchronous code is provided by a third-party-library that is not so easy to rewrite. However, since Nodejs provides "synchronous" functions like "readFileSync", I thought that this is an easy API-conversion. – Mike76 Oct 20 '20 at 15:56

0 Answers0