Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node exits before async function completes

I have a function that returns a promise, and I am trying to await on it from within an async function. The problem is that the program completes immediately, instead of awaiting the promise.

async-test.js:

function doItSlow() {
    const deferred = new Promise();

    setTimeout( () => {
        console.log( "resolving" );
        deferred.resolve();
    }, 1000 );

    return deferred;
}

async function waitForIt( done ) {

    console.log( "awaiting" );
    await doItSlow();
    console.log( "awaited" );
    done();
}

waitForIt(() => {
    console.log( "completed test" );
});

console.log( "passed by the test" );

Build and run:

babel --stage 0 --optional runtime async-test.js > at.js && node at.js`

Result:

awaiting
passed by the test

Resolving immediately instead of waiting one second has no effect:

function doItSlow() {
    const deferred = new Promise();

    console.log( "resolving" );
    deferred.resolve();

    return deferred;
}

Interestingly, "resolving" is never printed, even though it is now synchronous:

awaiting
passed by the test

I would suspect a compiler problem, but I checked Babel's output and sure enough, it did compile the synchronous version.

I thought I could just await on a promise from within an async function. Is there something I am missing here?

like image 319
Dan Ross Avatar asked Jun 29 '15 20:06

Dan Ross


People also ask

Does async function return immediately?

With that design, you call the asynchronous function, passing in your callback function. The function returns immediately and calls your callback when the operation is finished. With a promise-based API, the asynchronous function starts the operation and returns a Promise object.

How do you wait for an async function to finish?

The await operator is used to wait for a Promise. It can be used inside an Async block only. The keyword Await makes JavaScript wait until the promise returns a result. It has to be noted that it only makes the async function block wait and not the whole program execution.

Do async functions automatically return a promise?

Async functions are available natively in Node and are denoted by the async keyword in their declaration. They always return a promise, even if you don't explicitly write them to do so. Also, the await keyword is only available inside async functions at the moment – it cannot be used in the global scope.

Does async await stop execution?

The await expression is usually used to unwrap promises by passing a Promise as the expression . This causes async function execution to pause until the promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment.


1 Answers

You are not using Promise right (assuming it's standard compliant). It doesn't have resolve method. You should pass a function instead:

function doItSlow() {
  return new Promise(resolve => {
    setTimeout( () => {
      console.log( "resolving" );
      resolve();
    }, 1000 );
   }); 
 }
like image 163
vkurchatkin Avatar answered Oct 05 '22 22:10

vkurchatkin