JSFiddle - React, Tailwind, and code Playground
by danShumway
JavaScript
var uniqueId = window ? window.performance.now.bind(window.performance)
: process.hrtime.bind(process);
var PRIVATE = uniqueId();
function Group(promise) {
_that = this[PRIVATE] = {
//Do I need a parent?
__proto__ : Group._prototype,
promise : promise || Promise.resolve()
};
_that[PRIVATE] = _that;
}
Group._prototype = {
wrap : function (assertion) {
if (typeof assertion === 'function') {
return assertion;
}
return function () {
return assertion;
};
},
promisify : function (result, group) {
var _that = this[PRIVATE];
if (result == null) {
return Promise.resolve();
}
if (result instanceof Promise) {
return result;
}
if (typeof result === 'function') {
//For the API to be completely, 100% consistent, allow recursion.
return _that.promisify(result(group), group);
}
return result ? Promise.resolve() : Promise.reject();
}
};
Group.prototype = {
test : function (message, assertion) {
var _that = this[PRIVATE];
assertion = _that.wrap(assertion);
//TODO: you need a cleaner way to do this than a `skip` variable
var skip = false;
var promise = _that.promise.then(function () {
//No need to catch exceptions since the promise we're in will handle them.
//https://promisesaplus.com/#point-34 - `promise` will be written correctly by execution time
var group = new Group(promise);
return _that.promisify(assertion(group), group);
}, function (err) {
console.log(message, 'assertion prevented');
skip = true;
throw err;
});
promise.then(function () {
if (!skip) {
console.log(message, 'passed');
}
}, function (err) {
if (!skip) {
...