Promises

by Anton

JavaScript

const promise = new Promise((resolve, reject) => {
	console.log('inside promise');
    //throw new Error('Error in promise');
	setTimeout(() => resolve(0), 300);
});

promise
	.then(val => {
    	console.log('1st then:', val);
        //throw new Error('Error in 1st then'); // caught
        //nonExisting(); // caught
        return new Promise(resolve => {
        	console.log('inner promise');
        	setTimeout(() => resolve(1), 300);
        });
	})
    .then(val => {
    	console.log('2nd then:', val);
        return 2;
	})
    .catch(err => {
    	console.log('catch:', err);
        return 3;
	})
    .then(val => console.log('then after catch:', val));
    
console.log('---------- end ---------');