Skip to content

Instantly share code, notes, and snippets.

@crongro
Created April 25, 2017 06:57
Show Gist options
  • Save crongro/af9488cdee2c64c4905cea85c1393b1a to your computer and use it in GitHub Desktop.
Save crongro/af9488cdee2c64c4905cea85c1393b1a to your computer and use it in GitHub Desktop.
simple Promise
//SimplePromise
function SimplePromise(fn){
let resolveData = null;
let rejectData = null;
let thenCallback = null;
const resolve = function(resultData) {
resolveData = resultData;
setTimeout(()=>{thenCallback(resolveData)},0);
}
let reject = function(resultData) {
rejectData = resultData;
}
const then = function(cb) {
thenCallback = cb;
}
fn(resolve, reject);
return {
resolveData, rejectData, then
}
}
//Use Promise.
const result = SimplePromise(function(resolve, reject){
setTimeout(() => {resolve("aaaa")}, 1000);
reject("bbbbb")
});
result.then(function(data){
console.log('data is ', data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment