11

I'm using a function that extracts data from a GitHub repository. It returns an object with metrics like the number of closed issues, etc. This function is passed as a parameter of another function that stores these metrics in a database.

store(extract());

The problem is that the extract function is asynchronous (and for some reason, it needs to be asynchronous) and is not returning values... I don't know how to manage async very well. How I force store() to wait for extract() return the metrics?

Thanks in advance.

3
  • You can only deal with the Promise extract produces or make store asynchronous as well and use await. Which is basically the same thing, anyway, as await is still using the Promise. Commented Mar 17, 2020 at 19:20
  • How I force store() to wait for extract() return the metrics? --> store(await extract()) if the containing function is async Commented Mar 17, 2020 at 19:21
  • How do I declare an export an async? Example: exports.extract = () => {} Commented Mar 17, 2020 at 20:57

4 Answers 4

20

I ended up here trying to find a solution to a similar problem, so for other unlucky lads like me, gonna paste what I figured out, the following works:

async function A(B: () => Promise<void>) {
    await B();
}

Now I want to call A and pass an async function, then I do it like this:

await A(async () => {
    await wait(3000);
})
Sign up to request clarification or add additional context in comments.

Comments

1

Async function is nothing but a function return promise. take sample.

const getPromise = () =>  Promise.resolve("1")

const store = (fn) => {
  fn().then(console.log)
}
store(getPromise)

const storeCB = (fn, cb) => {
  fn().then(cb)
}
store(getPromise, console.log)

const storeThen = (fn) => {
  return fn().then(x => "append: " + x)
}
storeThen(getPromise).then(console.log)

const getAsync = async () =>  "2"

store(getAsync)


const storeWithAwait = async (fn) => {
  const restult = await fn()
  return restult
}

storeWithAwait(getAsync).then(console.log)

Comments

0

Did you try something like this?

(async ()=>{
   const result = await extract();
   store(result);
})()

3 Comments

Or store(await extract()).
How do I declare an export an async? Example: exports.extract = () => {}
if you only have 1 script to export then export default async function(){}, if you have more things to export then you can do it like this: first declare your function for example async function myFunction(){} then you can export it export { myFunction }; and later you import it import { myFunction } from "path/to/script"
0

Either use an async IIFE like this:

(async () => {
   const result = await extract();
   store(result);
})();

Or use the classic syntax:

extract().then(store);

2 Comments

Or just extract().then(store).
How do I declare an export an async? Example: exports.extract = () => {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.