1 2function runAsync(x) { 3 const p = new Promise(r => 4 setTimeout(() => r(x, console.log(x)), 1000) 5 ); 6 return p; 7} 8function runReject(x) { 9 const p = new Promise((res, rej) => 10 setTimeout(() => rej(`Error: ${x}`, console.log(x)), 1000 * x) 11 ); 12 return p; 13} 14Promise.race([runReject(0), runAsync(1), runAsync(2), runAsync(3)]) 15 .then(res => console.log("result: ", res)) 16 .catch(err => console.log(err)); 17