# Detect when JS function has completed

**URL:** <https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474>\
**Category:** General\
**Created:** [September 24, 2020, 11:59am UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474 "2020-09-24T11:59:20Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 24, 2020, 11:59am UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/1 "2020-09-24T11:59:20Z")

</div>

I’m building a React Guitar, and on page load, I’m creating the wav files for the sounds, which takes a couple of seconds. I’m unsure how to implement something like a “loading” bar.

I’ve struggled with this problem in several projects but never quite got my head around it. Do I need await/async or promises for that?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 24, 2020, 2:37pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/2 "2020-09-24T14:37:38Z")

</div>

Without seeing any code it would be hard to suggest something specific, but, yes, you’ll need either await/async, promises or callback.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 24, 2020, 6:22pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/3 "2020-09-24T18:22:32Z")

</div>

I have a codesandbox here: [https://codesandbox.io/s/condescending-black-1lc9z](https://codesandbox.io/s/condescending-black-1lc9z)

So, in the utils folder, you’ll find a `playGuitar.js`., which imports `audioFiles.js`, which exports an object with references to all wavs.

I’d be interested in the callback solution, how would I write that?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 24, 2020, 7:01pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/4 "2020-09-24T19:01:30Z")

</div>

Here’s an example with a callback (check console): [https://codesandbox.io/s/headless-frost-ce8rs](https://codesandbox.io/s/headless-frost-ce8rs) (on my computer file loading takes less than 300ms).

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 24, 2020, 7:09pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/5 "2020-09-24T19:09:43Z")

</div>

Awesome 😃  
Digesting that with pen & paper will open some eyes over here

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 25, 2020, 11:26am UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/6 "2020-09-25T11:26:37Z")

</div>

After making it work with a callback, I’m now trying to do the same with async/await instead. For easier reference, here’s the setup:

_createAudioFiles.js_

```auto
export function createAudioFiles(){
    // big block of code
    return fileCache
}

```

_playGuitar.js_

```auto
import {createAudioFiles} from '../utils/createAudioFiles.js';

/* this is what I tried: */
const getAudio = async function(){
    let result = await createAudioFiles();
    console.log(result) // result is an empty fileCache
    return result;
}
const audioFiles = getAudio();

console.log(audioFiles); // Promise(pending)

```

I’m obviously missing something. I’ve read that even if my async function doesn’t explicitly return a Promise, it will automatically be wrapped in a Promise that will resolve to the value it’s returning.

Why is `result` an empty `fileCache`? Shouldn’t the _await_ keyword make sure that `result` is assigned AFTER the complete `fileCache` has been created and returned from my _createAudioFiles_ function?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 25, 2020, 11:46am UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/7 "2020-09-25T11:46:59Z")

</div>

> [@jsdisco](#):
>
> if my async function doesn’t explicitly return a Promise, it will automatically be wrapped in a Promise

Yes, but `createAudioFiles` is not an async function.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 25, 2020, 1:16pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/8 "2020-09-25T13:16:56Z")

</div>

Ugh. Alright I’ve modified the code like this:

_createAudioFiles.js_

```auto
export async function createAudioFiles(){
    // big block of code
    return await fileCache
}

```

It doesn’t help. In fact I don’t even see a difference in my `console.logs` (I’ve added another one where I log `audioFiles` with a timeout after 2 seconds). At that point, it’s a Promise with status fulfilled. But when I inspect it with the dev tools, it still holds an empty fileCache. I think I need to “unwrap” that Promise somehow to get my fileCache. I just don’t know how.

I thought I’m doing it exactly like it is described in the first example of the docs:  
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async\_function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)

I actually find that particular article terribly confusing. It’s not the first time I’m digging my way through it but I just. don’t. get. it.

They write that using async/await allows it to write async code without the explicit use of Promises. But later in all their examples, they’re doing exactly that.

They also write that the difference between `return fileCache` and `return await fileCache` is that `return await` will WAIT for fileCache to resolve. Why isn’t that happening in my code?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 25, 2020, 1:57pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/9 "2020-09-25T13:57:53Z")

</div>

Here’s an async/await version: [green-resonance-1hil2 - CodeSandbox](https://codesandbox.io/s/green-resonance-1hil2) (I added 2s artificial delay)

> [@jsdisco](#):
>
> They also write that the difference between `return fileCache` and `return await fileCache` is that `return await` will WAIT for fileCache to resolve. Why isn’t that happening in my code?

Async only awaits for promises.

---

<div class="post-metadata">

**Author:** ![chuckadams](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chuckadams/32/109651_2.png) [@chuckadams](https://forum.freecodecamp.org/u/chuckadams)\
**Post date:** [September 25, 2020, 2:07pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/10 "2020-09-25T14:07:41Z")

</div>

`return await foo` is redundant. In an async function, it means exactly the same thing as `return foo`, and in a non-async function, it’s a compile error.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 25, 2020, 2:16pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/11 "2020-09-25T14:16:31Z")

</div>

> [@jenovs](#):
>
> Here’s an async/await version: [green-resonance-1hil2 - CodeSandbox](https://codesandbox.io/s/green-resonance-1hil2) (I added 2s artificial delay)
> 
> Async only awaits for promises.

You’re giving me headaches 🥴

Why are you returning a promise from createAudioFiles() now? I thought that wasn’t necessary with async/await?

Thanks for the edit, nonetheless. I guess I’ll have to learn it the hard way, which means going through the docs and code along with every single example until it finally enters my brain.

> [@chuckadams](#):
>
> `return await foo` is redundant. In an async function, it means exactly the same thing as `return foo`, and in a non-async function, it’s a compile error.

Not really, here’s the part from the docs:

> This highlights the subtle difference between `return foo;` and `return await foo;` — `return foo` immediately returns `foo` and never throws, even if `foo` is a Promise that rejects. `return await foo` will _wait_ for `foo` to resolve or reject if it’s a Promise, and throws **before returning** if it rejects.

Probably only relevant when you have to handle errors but still good to know.

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 25, 2020, 2:30pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/12 "2020-09-25T14:30:22Z")

</div>

> [@jsdisco](#):
>
> Why are you returning a promise from createAudioFiles() now?

The promise in `createAudioFiles` is just to create an artificial delay. It is not returned.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 25, 2020, 2:57pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/13 "2020-09-25T14:57:10Z")

</div>

OH alright this explains why it takes two seconds longer for the files to be created/the function to return.

I’m still going through your edits and I’m still super confused, for example as to why you’ve moved the execution of _createAudioFiles()_ into my _createGuitarNotes()_ function, those two are totally unrelated, and I’m still puzzled why my fileCache always seems to be just an empty fileCache when I log it, no matter where or when I log it…

Brain just needs more time (await click)

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [September 25, 2020, 3:04pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/14 "2020-09-25T15:04:17Z")

</div>

> [@jsdisco](#):
>
> why you’ve moved the execution of _createAudioFiles()_ into my _createGuitarNotes()_ function

Because you can only await inside async function. Another option would be to create an IIFE.

> [@jsdisco](#):
>
> those two are totally unrelated

`createGuitarNotesArr` is the only place where `audioFiles` is used, so to me it looks like they are totally related.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [September 25, 2020, 3:32pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/15 "2020-09-25T15:32:53Z")

</div>

> [@jenovs](#):
>
> Because you can only await inside async function. Another option would be to create an IIFE.

That’s why I had created a small `getAudio()` function in my original approach, so at least I got _that_ half-right… I’ve merged your solution into that, because

> [@jenovs](#):
>
> `createGuitarNotesArr` is the only place where `audioFiles` is used, so to me it looks like they are totally related.

isn’t quite right, _createGuitarNotesArr()_ only creates an array for the _fretboardValsToNote()_ function (sort of a look-up table), while my `audioFiles` are used by the _playNote_ function which in turn is used by all functions I’m exporting, … but I’m just nitpicking here.

Point is it works now with my modified _getAudio()_ function.

This works:

```auto
let audioFiles;
const getAudio = async function(){
    audioFiles = await createAudioFiles();
}
getAudio();

```

This doesn’t:

```auto
const getAudio = async function(){
    let result = await createAudioFiles();
    return result
}
const audioFiles = getAudio();

```

I’m not 100% sure I’ve understood why yet, but it’s good to know what I’ve done wrong (here, and whenever I tried something with async/await before).

Also found out why my fileCache seemed to be empty. I’m just stupid, some entries _must_ be empty because a guitar doesn’t have all the notes that a full 88-key piano has, and the fileCache is built with identical structure for both instruments.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://forum.freecodecamp.org/t/detect-when-js-function-has-completed/422474/17 "2021-06-01T18:38:38Z")

</div>

Closed on June 1, 2021, 6:38pm UTC


