0

This is in reference to npm libraries.

How do I know that a particular operation or function from the library is asynchronous, in which I have to use the async await syntax.

Vaibhav
  • 47
  • 1
  • 6
  • If you don't know, it's safe to treat it as though it is! – Tad Donaghe May 04 '18 at 17:04
  • 8
    Read the documentation – Feathercrown May 04 '18 at 17:04
  • Does the function take a callback or return a promise. If so it is almost certainly asynchronous. – bhspencer May 04 '18 at 17:06
  • These features are sort of stitched together in JS. They decided to have the standard library include a `Promise` library, which then became the basis for `async/await`. So there's language-level functionality, but not language-level enforcement. –  May 04 '18 at 17:12
  • Would be nicer if JS decided to have `async` functions that couldn't be called without `await`, and that had a way for the `async` function to signal its completion and result without having this library in between. –  May 04 '18 at 17:14
  • I assume this is server-side code. You may be happier in a language that has first class support for concurrent/asynchronous code handling. We're stuck with JS in the browser (for now), but there are much more comfortable languages outside of that environment. –  May 04 '18 at 17:18
  • Possible duplicate of [How to know if a function is async?](https://stackoverflow.com/questions/38508420/how-to-know-if-a-function-is-async) – Heretic Monkey May 04 '18 at 17:25

1 Answers1

-1

Async/Await build on Promises, so functions that return promises can be awaited. If a function takes a callback it is typically asynchronous as well, but cannot be awaited. You can however transform such APIs to provide Promise objects and in turn await those.

pschichtel
  • 759
  • 7
  • 18