Skip to content

Instantly share code, notes, and snippets.

@AlexTiTanium
Last active November 5, 2015 07:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AlexTiTanium/d4e5828a4763a68c2b60 to your computer and use it in GitHub Desktop.
Tasks for review Write Promises
var domain = require('domain');
// Do not change this function
function veryBadFunction(){
setTimeout(function(){
throw new Error('oops!!!');
}, 0)
}
var d = domain.create();
d.on('error', function(er) {
console.log('error');
});
d.run(function(){ veryBadFunction(); });
// Реализовать метод `.delay`
var foo = function () {
console.log("bingo!");
};
foo.delay(300); // после 0.3s: "bingo!"
bar.delay(600); // после 0.6s: "Wow!"
function bar() {
console.log("Wow!");
}
// Реализовать класса Futures
function Futures(executor) {
}
Futures.prototype.then = function () {
};
// Тест #1
var foo = new Futures(function (resolve, reject) {
resolve(1);
});
foo.then(function (val) {
console.info("foo.resolved:", val === 1);
}, function () {
console.assert(false, "foo.resolved");
});
// Тест #2
var bar = new Futures(function (resolve, reject) {
setTimeout(resolve.bind(null, 2), 300);
setTimeout(reject.bind(null, 3), 200);
});
bar.then(function (val) {
console.assert(false, "bar.rejected");
}, function (val) {
console.info("bar.rejected:", val === 3);
});
// Do not change this function
function veryBadFunction(){
throw new Error('oops!!!');
}
try{
veryBadFunction();
}catch(e){
console.log(e.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment