Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resolve implicit Promise from async

All functions marked with "async" return a Promise object, even if it’s not returned explicitly. So I made a following experiment:

async function getPromise() {
    setTimeout(() => { return "value"; }, 2000);
}

async function implicitPromiseTest() {
        const val = await getPromise();
        console.log(val);                // prints "undefined"
}

implicitPromiseTest();    

In example 1, the value after "return" should be implicitly wrapped in a Promise. This Promise would then be returned from the function. I expected it to be resolved in the "implicitPromiseTest" function and the returned value – printed to console.

I gues this doesn’t work because the implicit Promise from "getPromise" never actually resolves. The proper way to do this task would be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

async function getPromise() {
    return new Promise((resolve, reject) => 
      { setTimeout(() => { resolve("value"); }, 2000);})
}

async function implicitPromiseTest() {
    const val = await getPromise();
    console.log(val);             // prints "value"
}

But this raises questions:

  1. How can I resolve a Promise returned implicitly from an async function, without explicitly returning a Promise from that function’s block? Such feature would allow me to simplify my code substantially.

  2. If this can’t be done, then what even is the point of async always implicitly returning Promises? What’s the use of those Promises if they can’t be resolved or rejected?

  3. In my 2nd function, the "await" keyword should halt function execution until the Promise from "getPromise()" is resolved. And since it never resolves, the execution should stop there indefinitely. Then why does the "console.log" execute?

>Solution :

In example 1, the value after "return" should be implicitly wrapped in a Promise.

It’s only going to wrap a promise around things you return from the async function. A value returned in a setTimeout callback function is going to be ignored.

So what’s happening is: your async function starts running, then sets up a timeout, then implicitly returns undefined. Since this is an async function, that undefined gets wrapped in a promise. That promise is in a resolved state, since the function is done executing. Some time later the timeout goes off, but that has no effect on the async function which has already finished resolving its promise.

async/await exists to simplify the code for working with already-existing promises. It does not take non-promise code and convert it to promise code, except for the case where you return a non-promise. If you want to create a promise from setTimeout or any other callback-based function, then you must use the new Promise constructor.

what even is the point of async always implicitly returning Promises?

So you don’t have to end all your async functions with return Promise.resolve(someValue). You can instead do return someValue

eg:

async function fetchStuff() {
  const response = await fetch('someurl');
  const data = await response.json();
  return data.userList; // instead of return Promise.resolve(data.userList)
}

In my 2nd function, the "await" keyword should halt function execution until the Promise from "getPromise()" is resolved. And since it never resolves […]

That’s a false premise. In your first example the promise resolves right away, in your second example it resolves when the timeout goes off. Now that it’s resolved, any code that was awaiting it can continue

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading