Created
April 25, 2017 06:57
-
-
Save crongro/af9488cdee2c64c4905cea85c1393b1a to your computer and use it in GitHub Desktop.
simple Promise
This file contains hidden or 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
//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