JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<script src="https://dl.dropboxusercontent.com/u/7909102/log.js"></script>

JavaScript

function delay(t, val, doReject) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            if (doReject) {
                log("reject: ", val);
                reject(val);
            } else {
                log("resolve: ", val);
            	resolve(val);
            }
        }, t);
    });
}

function setDelay(t, val, doReject) {
    return function() {
        return delay(t, val, doReject);
    }
}


// first example where a throw in the reject handler stops
// further success processing
log("starting first example - throw from reject handler");
delay(100, 1, false).then(setDelay(100, -2, true))
    .then(function(val) {
    	log("success: ", val);
    	return val;
	}, function(err) {
    	log("error: ", err);
        // throw here to stop further processing
    	log("throwing from reject handler");
    	throw err;
	}).then(function(val) {
    	log("final success: ", val);
	}, function(err) {
    	log("final err: ", err);
	});

// these are wrapped in a setTimeout only to make sure the debug output 
// for the separate runs is separated
setTimeout(function() {
    log("<hr>");
	log("starting second example - return value from reject handler");
    // second example where a throw in the reject handler stops
    // further success processing
    delay(100, 1, false).then(setDelay(100, 1, true))
        .then(function(val) {
            log("success: ", val);
            return val;
        }, function(err) {
            log("error: ", err);
            // return value here
            // rejection will be considered handled and 
            // further success processing will continue
        	log("returning regular value from reject handler");
            return err;
        }).then(function(val) {
            log("final success: ", val);
        }, function(err) {
            log("final err: ", err);
        });
}, 1000);


setTimeout(function() {
    log("<hr>");
	log("starting third example - missing reject...