Does Awaiting a Non-Promise Have Any Detectable Effect

Does awaiting a non-Promise have any detectable effect?

await is not a no-op. If the awaited thing is not a promise, it is wrapped in a promise and that promise is awaited. Therefore await changes the execution order (but you should not rely on it nevertheless):

The following outputs 1, 2, 3:

console.log(1);

(async function() {
var x = await 5;
console.log(3);
})();

console.log(2);

No typescript warning when awaiting non Promise function

This is by design, so don’t expect Typescript to be raising the error you’re looking for any time soon.

However, TSLint can provide a warning or error for this case.

Execute promises after each other or apply timeouts to promise

What is happening:

const handleEditUser = async () => {
let p1, p2, p3

p1 = UserUtils.setProjects(user, client, newProjects) // request p1 starting

p2 = UserUtils.updateRights(user.id, userAuthority) // request p2 starting

p3 = UserUtils.updateUserClient(client, user) // request p3 starting

await Promise.all([p1, p2, p3]) // waiting for all of those to finish
// but requests are already fired

// or

await p1 // waiting for p1 to finish, p2 and p3 are running in parallel
await p2 // waiting for p2 to finish, p3 can still be running in parallel
await p3
}

Basically, when your UserUtils methods are actually starting the workflow of the promises. So you should rather consider wait for one promise to be over before starting the next workflow:

const handleEditUser = async () => {
await UserUtils.setProjects(user, client, newProjects)

await UserUtils.updateRights(user.id, userAuthority)

await UserUtils.updateUserClient(client, user)
}

Why reduce with async function returns only the last object

You're very close. The main issue is that you wrote the array as [(...)] instead of [...]. In your code, the array only contains one value, if you remove the () you will have three values. See comma operator for details.

The other thing you have to consider is that an async function always returns a promise, so you must await o as well -

(async () =>
console.log(
await ["apple", "banana", "orange"].reduce(async (o, fruit) => {
const rand = new Promise((resolve) => setTimeout(() => resolve(Math.random()), 200))
return { ...await o, [fruit]: await rand }
}, {})
))();

Typescript async/await with Observable Or Promise

You can use the async functions the way you do but it will have no effect in your example. You have to understand that async function returns a promise:

const fn = async function() {};
fn() instanceof Promise

So if you use it in the observable chain you will receive a promise:

interval(500)
.first()
.map(async (v) => {
return v;
})
.subscribe((v) => {
console.log(v instanceof Promise); // logs `true`
});

The way you constructed your example it won't have any effect on the observables chain because output from do operator is ignored and there's no listener for the output from the subscribe callback. So basically there's no reason to use async there.

If you wanted instead to wait until async function resolves and get the results of that function use mergeMap operator:

interval(500)
.first()
.map(async (v) => {
return v;
})
.mergeMap((p) => {
return p;
})
.subscribe((v) => {
console.log(v);
});


Related Topics



Leave a reply



Submit