Choose Your Asset

Sometimes you want to create an augmented reality (AR) experience but you get caught up in the technical details. Or maybe you don’t have the programming skills you think are needed. Good news…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Easy timeouts with Promises

Promises does not support timeouting an operation natively, so this is the simplest, most straight forward way of timeouting an operation i could think of.

Let´s have some operation that can take unspecified amout of time:

and now let´s create a timeout function, that accepts number of milliseconds after it times out and returns a promise:

Now we just need to add some logic that cancels the first promise after the second times out. You are in luck! Promises provide

that does exactly what we need. It runs all promises in parallel and settles to first promise that is resolved/rejected.

Now let´s run it:

But we can do even better! We can enhance our timeout function to accept a promise and time.

Now we can easily use it as:

That´s it. 😊 Happy coding!

Add a comment

Related posts:

MANSA MUSA

The Richest Person in History. “MANSA MUSA” is published by Pathofallah.