JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://wzrd.in/standalone/expect@latest"></script>
<body>
</body>
Babel + JSX
const promiseWhile = (data, condition, action) => {
var whilst = (data) => {
console.log(data);
return condition(data) ?
action(data).then(whilst) :
Promise.resolve(data);
}
return whilst(data);
};
const decrementTest = () => {
const decrement = (num) => {
return new Promise((resolve, reject) => {
resolve(num - 1);
});
}
promiseWhile(4, i => i > 0, decrement).then(i =>
expect(i).toEqual(0));
// Synchronous equivalent
let i = 4; // data
while (i > 0) { // condition
i = i - 1; // action
}
expect(i).toEqual(0);
};
const squareTest = () => {
const square = (num) => {
return new Promise((resolve, reject) => {
resolve(num * num);
});
}
promiseWhile(2, i => i < 16, square).then(i =>
expect(i).toEqual(256));
// Synchronous equivalent
let i = 2;
while (i < 17) {
i = i * i;
}
expect(i).toEqual(256);
};
[decrement, squareTest].forEach(test => test());
document.body.innerHTML = 'All tests passed';