Reputation: 99
Just wondering if this is do-able (I assume not because I have never seen it done anywhere) and if anyone has a good explanation as to why not.
const expiry = new Date(await getTokenExpiry() * 1000)
Or whether I have to do it like this:
const expiry = await getTokenExpiry()
const muhExpiry = new Date(expiry * 1000)
Thank you for reading my question.
Upvotes: 0
Views: 64
Reputation: 370679
Yes, it can be done - arguments are evaluated before their outer expressions, after all, which goes hand-in-hand with await
, should you need it. For example:
const multiplyBy4 = num => num * 4;
const resolveWithOne = () => new Promise(res => setTimeout(res, 500, 1));
(async () => {
console.log('start');
const result = multiplyBy4(await resolveWithOne());
console.log(result);
})();
That said, unless the expression is quite trivial, code would probably be a bit more readable if you put each await
on its own line.
Upvotes: 3