Skip to content

Instantly share code, notes, and snippets.

@dbalduini
Last active May 15, 2016 20:50
Show Gist options
  • Save dbalduini/c730cc7492c8a4c1662efcc8ff3926cd to your computer and use it in GitHub Desktop.
Save dbalduini/c730cc7492c8a4c1662efcc8ff3926cd to your computer and use it in GitHub Desktop.
Blocking Promise
'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