Reputation: 777
I was just trying different combinations of async await problem statements and I tried this,
basically I have two functions promise1()
and promise2()
which return two promises which resolve values 10 and 20 after 3 and 1 seconds respectively. Below is the code that I have written
function promise1()
{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(10)
},3000)
})
}
function promise2()
{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(20)
},1000)
})
}
async function sum()
{
let num1 = await promise1();
let num2 = await promise2();
return num1+num2;
}
sum().then(s => console.log(s))
as in the above code if I was able to apply .then()
to sum
then is it a promise?
when I did console.log(typeof sum)
its saying sum
is a function
not an object
so what exactly are async functions? I have tried searching the answer for this behaviour but couldn't find any sources that would correctly answer. It would be a great help for me if someone answered this query or tell me the online sources or books that would get me the answer
my node version is v10.15.3
thank you
Upvotes: 0
Views: 55
Reputation: 1074355
Are async functions really functions or Promise objects?
They're really functions. The functions return promises.
as in the above code if I was able to apply
.then()
tosum
then is it a promise?
You didn't. You used .then()
on the result of calling sum
(sum().then(...)
, not sum.then(...)
). It's just like calling a function that returns a string, then using toUpperCase
on what the function returns.
These two functions are essentially equivalent, handwaving some minor details:
// #1
function foo() {
return new Promise((resolve, reject) => {
try {
getSomePromise()
.then(result => result * 2)
.catch(reject);
} catch (error) {
reject(error);
}
});
}
and
// #2
async function foo() {
const result = await getSomePromise();
return result * 2;
}
Naturally, that's not how you'd probably write #1 in real life, it's an example of the promise creation antipattern, but it's a reasonable interpretation of what an async
function looks like under the hood. Purely for completeness, you'd probably write #1 like this:
// #3
function foo() {
return getSomePromise().then(result => result * 2);
}
The difference is that if getSomePromise
throws an error rather than running and returning a promise, that function (#3) will throw an error (synchronously), whereas #1 above will return a promise that rejects instead. async
functions do the latter, which is why #1 is written the way it is above.
Upvotes: 2
Reputation: 943561
They are functions where the return value is always a promise.
async function foo() { }
const value = foo();
console.log(foo instanceof Function);
console.log(foo instanceof Promise);
console.log(value instanceof Function);
console.log(value instanceof Promise);
as in the above code if I was able to apply .then() to sum then is it a promise?
No, you weren't.
sum.then()
won't work. sum().then()
will. Adding ()
will call a function and give you the return value.
Upvotes: 3