Skip to main content
added 360 characters in body
Source Link
rsp
  • 112.1k
  • 31
  • 209
  • 185

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

SeeOr, using an IIFE - thanks to Robert Klep for the docssuggestion:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

void async function main() {
    // use await here
}();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await Or, if you like punctuation:

(async () => {
    // use await here
})();

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however, as it would have the same effect as running kill -STOP with your own process ID and waiting for the stopped process to resume itself.

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however, as it would have the same effect as running kill -STOP with your own process ID and waiting for the stopped process to resume itself.

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

Or, using an IIFE - thanks to Robert Klep for the suggestion:

void async function main() {
    // use await here
}();

Or, if you like punctuation:

(async () => {
    // use await here
})();

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however, as it would have the same effect as running kill -STOP with your own process ID and waiting for the stopped process to resume itself.

added 360 characters in body
Source Link
rsp
  • 112.1k
  • 31
  • 209
  • 185

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however, as it would have the same effect as running kill -STOP with your own process ID and waiting for the stopped process to resume itself.

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however.

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however, as it would have the same effect as running kill -STOP with your own process ID and waiting for the stopped process to resume itself.

added 360 characters in body
Source Link
rsp
  • 112.1k
  • 31
  • 209
  • 185

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however.

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Is there a way to make the javascript await keyword work outside async functions?

Sadly, the answer is: No.

I would like to be able to freeze the entire call-stack (instead of just the rest of the async function), to be resumed once the particular promise returns a value.

But how could your promise return a value if the entire call-stack was frozen? It would be stuck forever.

More details

To use await you need to be inside async function.

At least you have to do:

async function main() {
  // use await here
}
main();

in your main code.

See the docs:

The await expression causes async function execution to pause, to wait for the Promise's resolution, and to resume the async function execution when the value is resolved. [emphasis added]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Other options

There are some other options to handle concurrency in Node, like:

none of which will freeze the call stack, however.

added 280 characters in body
Source Link
rsp
  • 112.1k
  • 31
  • 209
  • 185
Loading
Source Link
rsp
  • 112.1k
  • 31
  • 209
  • 185
Loading