Implement a Promise that is Promises/A+ compliant using vanilla JavaScript.
const Promise = require("./promise");
Promise.resolve(233)
.then()
.then(function(value) {
console.log(value);
});
function delay(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
(async function print() {
console.time("setTimeout");
await delay(1000);
console.timeEnd("setTimeout");
})();
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, "foo");
});
const promise4 = new Promise(function(resolve, reject) {
setTimeout(resolve, 200, "bar");
});
Promise.all([promise1, promise2, promise3, promise4]).then(function(values) {
console.log(values);
});
const promise1 = new Promise(function(resolve, reject) {
setTimeout(resolve, 500, "one");
});
const promise2 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, "two");
});
Promise.race([promise1, promise2]).then(function(value) {
console.log(value);
});
npm run test
Note: The Promises/A+ compliance test suite can be found here.