Last active
November 1, 2018 08:01
-
-
Save ibreathebsb/bb52506e27c422e80e5b1f60911bda33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Promise(exec) { | |
const self = this; | |
self.status = "pending"; | |
self.value = undefined; | |
self.onResolvedCallbacks = []; | |
self.onRejectedCallbacks = []; | |
function resolve(value) { | |
if (self.status === "pending") { | |
setTimeout(() => { | |
self.status = "resolved"; | |
self.value = value; | |
while ((cb = self.onResolvedCallbacks.shift())) { | |
cb(value); | |
} | |
}); | |
} | |
} | |
function reject(reason) { | |
if (self.status === "pending") { | |
setTimeout(() => { | |
self.status = "rejected"; | |
self.value = reason; | |
while ((cb = self.onRejectedCallbacks.shift())) { | |
cb(reason); | |
} | |
}); | |
} | |
} | |
try { | |
exec(resolve, reject); | |
} catch (e) { | |
reject(e); | |
} | |
} | |
function empty(v) { | |
return v | |
} | |
Promise.prototype.then = function(onResolve = empty, onReject = empty) { | |
const self = this; | |
const { status, value } = self; | |
if (status === "resolved") { | |
return new Promise(function(resolve, reject) { | |
try { | |
const res = onResolve(value); | |
if (res instanceof Promise) { | |
res.then(resolve, reject); | |
} else { | |
resolve(res); | |
} | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} else if (status === "rejected") { | |
return new Promise(function(resolve, reject) { | |
try { | |
const res = onReject(value); | |
if (res instanceof Promise) { | |
res.then(resolve, reject); | |
} else { | |
reject(res); | |
} | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} else { | |
return new Promise(function(resolve, reject) { | |
self.onResolvedCallbacks.push(function(v) { | |
try { | |
const res = onResolve(v); | |
if (res instanceof Promise) { | |
res.then(resolve, reject); | |
} else { | |
resolve(res); | |
} | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
self.onRejectedCallbacks.push(function(reason) { | |
try { | |
const res = onReject(reason); | |
if (res instanceof Promise) { | |
x.then(resolve, reject); | |
} else { | |
reject(res); | |
} | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
}); | |
} | |
}; | |
Promise.prototype.catch = function (onReject) { | |
return this.then(undefined,onReject) | |
} | |
var getData100 = function() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve("100ms"); | |
}, 1000); | |
}); | |
}; | |
var getData200 = function() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve("200ms"); | |
}, 2000); | |
}); | |
}; | |
var getData300 = function() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
reject("reject"); | |
}, 3000); | |
}); | |
}; | |
getData100() | |
.then(function(data) { | |
console.log(data); // 100ms | |
return getData200(); | |
}) | |
.then(function(data) { | |
console.log(data); // 200ms | |
return getData300(); | |
}) | |
.then( | |
function(data) { | |
console.log(data); // 100ms | |
}, | |
function(data) { | |
console.log(data); | |
// throw new Error('boom!') | |
} | |
).catch(e => { | |
console.log(e) | |
}) | |
; | |
// var m = new Promise((resolve, reject) => { | |
// resolve(1); | |
// }); | |
// m.then(() => { | |
// console.log(1); | |
// }); | |
// m.then(() => { | |
// console.log(2); | |
// }); | |
// console.log("sync"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment