Back
Close

JavaScript promises, mastering the asynchronous

Magus
38.4K views
Previous: How to join promises Next: The last challenge

You are now towards the end of this playground. However, the wild outside can be dangerous. So don't leave without a few tricks!

Test if an object is a promise

If you really have to, this is how you can test if an object is a promise : obj instanceof Promise. The problem is that this test only works with a real Promise object. It will not work with the Q library or with an Angular promise.

Promise.race

Promise.race takes an array of promises. The result is a new promise that resolves or rejects as soon as one of the promises in the given array resolves or rejects.

Promise.race example
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time, 'success ' + time);
});
}
Promise.race([delay(500), delay(100)]).then(function(data) {
console.log(data);
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants