Skip to content

Commit

Permalink
Simplify then() core
Browse files Browse the repository at this point in the history
  • Loading branch information
i20 committed Apr 3, 2020
1 parent 4848033 commit 9c90987
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
18 changes: 5 additions & 13 deletions Promise.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,16 @@ function Promise (_executor) {

const promise = new Promise((nextResolve, nextReject, nextNotify) => {

let result;
let success;
// /!\ If run() is called manually on the new promise whereas parent promise has not
// solved yet then new promise will be rejected with an undefined value

try {
result = (_state === STATE_RESOLVED ? resolve || noopResolve : reject || noopReject)(_value);
success = true;
}

catch (error) {
result = error;
success = false;
}
const result = (_state === STATE_RESOLVED ? resolve || noopResolve : reject || noopReject)(_value);

// If handler's result is a promise then, whether it was thrown or returned, it "replaces" the current promise
// If handler returns a promise then, it "replaces" the current promise
if (result instanceof Promise)
result.then(nextResolve, nextReject, nextNotify);

else (success ? nextResolve : nextReject)(result);
else nextResolve(result);
});

// Parent promise is not solved yet
Expand Down
18 changes: 5 additions & 13 deletions Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,16 @@ function Promise (_executor) {

var promise = new Promise(function (nextResolve, nextReject, nextNotify) {

var result;
var success;
// /!\ If run() is called manually on the new promise whereas parent promise has not
// solved yet then new promise will be rejected with an undefined value

try {
result = (_state === STATE_RESOLVED ? resolve || noopResolve : reject || noopReject)(_value);
success = true;
}

catch (error) {
result = error;
success = false;
}
var result = (_state === STATE_RESOLVED ? resolve || noopResolve : reject || noopReject)(_value);

// If handler's result is a promise then, whether it was thrown or returned, it "replaces" the current promise
// If handler returns a promise then, it "replaces" the current promise
if (result instanceof Promise)
result.then(nextResolve, nextReject, nextNotify);

else (success ? nextResolve : nextReject)(result);
else nextResolve(result);
});

// Parent promise is not solved yet
Expand Down

0 comments on commit 9c90987

Please sign in to comment.