3

Is it possible await to a function that is not a promise? Maybe a function that cannot fail.

for example:

async function doWork() { const newData = await cannotFail(); console.log("done: "+ newData); } function cannotFail() { var data = //cannot fail code . . . return data; } doWork(); 

I was thinking maybe with another syntax this option makes sense.

3
  • 1
    what's the point of awaiting a non-async function? If cannotFail function contains an async code that is callback-based, you can wrap a promise around it and return that promise from the cannotFail function. Commented Jul 12, 2021 at 16:30
  • 1
    you can actually await a non-promise, the await just does nothing. May be useful if something may or may not return a Promise dependant on some condition. Commented Jul 12, 2021 at 16:32
  • Async is not the same as try catch and has less to do with failing/non failing functions. It is for asynchronous code. Your function will run very well, but you do not need an await. Remove that and it is still the same. Maybe you want to ask something else? Commented Jul 12, 2021 at 16:36

3 Answers 3

5

Consider these two examples

async function getData(){ return "some data"; } async function doSomething() { const data = await getData(); console.log(data); } doSomething()

function getData(){ return "some data"; } async function doSomething(){ const data = getData(); console.log(data); } doSomething();

The result is exactly the same. You only use await when you want to wait for an asnyc function like it is synchronous. Normal functions already are synchronous.

And yes it is possible to use await on a normal function

function getData(){ return 'some data'; } async function doSomething(){ const data = await getData(); console.log(data); } doSomething();

There is just no reason to.

Sign up to request clarification or add additional context in comments.

1 Comment

I found an example where this is useful: I wrote a ModalProvider in React, and it receives an "onConfirm" callback. We want to close the modal after the call to onConfirm(), but we do not know if onConfirm is async => await onConfirm();closeModal()
1

It is possible to use await with an expression that does not evaluate to a promise. Whether that is useful, is questionable.

First of all, when await is followed by an expression that does not evaluate to a promise object, it is turned into one -- a resolved promise.

The main difference you get by using await with a non-promise or a resolved promise, is that await will make the async function return. This behaviour is not dependent on whether the promise is already resolved or not. await always postpones the further execution of the function by placing that resuming-job on the promise job queue.

See the difference here:

async function test() { let a = 1; // Without await console.log(a); return 2; } test().then(console.log); console.log("end of main, synchronous code");

And with await

async function test() { let a = await 1; // With await console.log(a); return 2; } test().then(console.log); console.log("end of main, synchronous code");

Note the different order of execution.

4 Comments

Ok, its probably what I meant, so if I dont specify that a function is async it will wait for it anyway? and why the second code is different? the console.log need to wait to the a variable in order to log it even if you dont add the await.
That are several questions. "If I don't specify that a function is async, it will wait for it either way?". If you refer to the expression that follows await, the rule is: if that expression is a promise (which is the case when you call an async function there), then await will await that promise to be resolved. It the expression does not evaluate to a promise (which is the case when you call a non-async function there), then await will treat that expression as a resolved promise. It will still make the function return first.
"why the second code is different?": because await always makes the wrapping async function return. The remaining code that follows below an await is never executed immediately. Never. That remaining code is always called from the event queue.
"the console.log need to wait to the a variable": that is an odd way to phrase it. The code that follows below an await (like that console.log) is never executed immediately. The function's execution context is saved when an await executes. It has nothing to do with the expression that the await operates on. It can be anything. It can be null, it can be 1, it can be the evaluation of a function. Whatever it is, await will make the wrapping function to return. Always.
0

It's not necessary, but it is possible:

const newData = await Promise.resolve( cannotFail() ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.