Created
September 10, 2015 05:02
-
-
Save otarim/e93a63b23a25e58e0ee6 to your computer and use it in GitHub Desktop.
convert thunk into 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
| var promiseAble = function(fn){ | |
| return new Promise(function(resolve,reject){ | |
| fn(function(err,data){ | |
| if(err){ | |
| reject(err) | |
| }else{ | |
| if (arguments.length > 2) { | |
| data = slice.call(arguments, 1) | |
| } | |
| resolve(data) | |
| } | |
| }) | |
| }) | |
| } | |
| var _asyncFn = promiseAble(function(cb){ | |
| fs.readFile('./es.js',cb) | |
| }) | |
| _asyncFn.then(function(data){ | |
| console.log(data) | |
| },function(err){ | |
| console.log(err) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment