Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed to SuperPromise! Homework's been started. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/part-1-task-2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

396 changes: 396 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

148 changes: 137 additions & 11 deletions lib/SuperPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,158 @@
// NO REQUIRE

class SuperPromise {

constructor(executor) {
this.state = 'PENDING';
this.handlers = [];
this.value = null;

executor(this.resolve.bind(this), this.reject.bind(this));

}

then(onResolve) {
handle (handler) {
if (this.state === 'PENDING') {
this.handlers.push(handler);
} else {
if (this.state === 'FULFILLED' &&
typeof handler.onFulfilled === 'function') {
handler.onFulfilled(this.value);
}
if (this.state === 'REJECTED' &&
typeof handler.onRejected === 'function') {
handler.onRejected(this.value);
}
}
}


done (onFulfilled, onRejected) {
var self = this;
setTimeout(function () {
self.handle({
onFulfilled: onFulfilled,
onRejected: onRejected
});
}, 0);
}

catch(onRejection) {
then (onFulfilled, onRejected) {
var self = this;
return new SuperPromise(function (resolve, reject) {
return self.done(function (result) {
if (typeof onFulfilled === 'function') {
try {
return resolve(onFulfilled(result));
} catch (ex) {
return reject(ex);
}
} else {
return resolve(result);
}
}, function (error) {
if (typeof onRejected === 'function') {
try {
return resolve(onRejected(error));
} catch (ex) {
return reject(ex);
}
} else {
return reject(error);
}
});
});
}

catch(onRejected) {
return this.then(null, onRejected);
}

/* PART 2 */
static resolve() {
// Your code here...

fulfill(result) {
this.handlers.forEach(handle);
this.handlers = null;
this.state = 'FULFILLED';
this.value = result;
}

getThen(value) {
var t = typeof value;
if (value && (t === 'object' || t === 'function')) {
var then = value.then;
if (typeof then === 'function') {
return then;
}
}
return null;
}

static resolve(result) {
return new SuperPromise((resolve) => resolve(result));
}

static reject() {
// Your code here...
resolve(result) {
var self = this;
try {
var then = self.getThen(result);
if (then) {
doResolve(then.bind(result), resolve, reject);
return;
}
this.fulfill(result);
} catch (e) {
this.reject(e);
}
return this;
}

static all() {
// Your code here...
// нужно создавать static отдельно, иначе tasks не видит их как функцию
static reject(error) {
return new SuperPromise((reject) => reject(error));
}

static race() {
// Your code here...
reject(error) {
this.state = 'REJECTED';
this.value = error;
}

static doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function (value) {
if (done) return;
done = true;
onFulfilled(value);
}, function (reason) {
if (done) return;
done = true;
onRejected(reason);
})
} catch (ex) {
if (done) return;
done = true;
onRejected(ex);
}
}

static all (promises) {
var accumulator = [];
var ready = SuperPromise.resolve(null);

promises.forEach(function (promise, ndx) {
ready = ready.then(function () {
return promise;
}).then(function (value) {
accumulator[ndx] = value;
});
});

return ready.then(function () { return accumulator; });
}

static race() {}

/* PART 3 */
static queue() {

Expand All @@ -41,4 +164,7 @@ class SuperPromise {
}
}

module.exports = Promise; // TODO: kek
module.exports = SuperPromise; // TODO: kek



6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"task_2": "NODE_PATH=. node tasks/task_2.js",
"task_3": "NODE_PATH=. node tasks/task_3.js",
"task_4": "NODE_PATH=. node tasks/task_4.js",
"test_base": "part-1-task-2-test-1 lib/testPromise && npm run test_static",
"test_advanced": "part-1-task-2-test-2 lib/testPromise && npm run test_static",
"test_base": "part-1-task-2-test-1 lib/testPromise",
"test_advanced": "part-1-task-2-test-2 lib/testPromise",
"test_static": "part-1-task-2-test-3 lib/testPromise",
"test_bonus": "part-1-task-2-test-4 lib/testPromise",
"test": "npm run test_advanced && npm run test_bonus"
"test": "npm run test_advanced && npm run test_static && npm run test_bonus"
},
"dependencies": {
"part-1-task-2-test-1": "git://github.com/hse2016/part-1-task-2-test-1.git",
Expand Down