Last active
May 15, 2016 20:50
-
-
Save dbalduini/c730cc7492c8a4c1662efcc8ff3926cd to your computer and use it in GitHub Desktop.
Blocking 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
'use strict' | |
const Promise = require('bluebird'); | |
const fs = require('fs'); | |
const deasync = require('deasync'); | |
function readFile(f) { | |
return new Promise(function (resolve, reject) { | |
let file = fs.readFileSync(f); | |
resolve(file); | |
}).delay(5000) | |
} | |
function waitPromiseValue (p) { | |
var value; | |
let i = setInterval(() => { | |
if (p.isFulfilled()) { | |
value = p.value(); | |
} | |
}, 400); | |
while (!value) { | |
deasync.runLoopOnce(); | |
} | |
clearInterval(i); | |
return value; | |
} | |
let p = readFile('index.js'); | |
let v = waitPromiseValue(p); | |
console.log(v.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment