Skip to main content
Steven Merrill

Tagged “async”

  1. I promise you shouldn't fear rejection

    Pop quiz for node.js users — what does this program output if the promise resolves, and what is different if it fails to reject through its else branch?

    async function main() {
    	const promise = new Promise((resolve) => {
    		setTimeout(() => {
    			if (Math.random() > 0.5) {
    				console.log("We win");
    				resolve();
    			} else {
    				console.log("We lose");
    			}
    		});
    	});
    	await promise.catch(console.error);
    	console.log("Looks like we made it");
    }
    
    main();
    

See all tags.