@@ -814,28 +814,26 @@ A função `then` retorna uma instância da Promise, do qual é uma nova instân
814
814
815
815
Para ` then` , ele pode essencialmente ser visto como flatMap` :
816
816
817
- For ` then` , it can essentially be seen as ` flatMap` :
818
-
819
817
` ` ` js
820
- // three states
818
+ // árvore de estados
821
819
const PENDING = 'pending';
822
820
const RESOLVED = 'resolved';
823
821
const REJECTED = 'rejected';
824
- // promise accepts a function argument that will execute immediately .
822
+ // promise aceita um argumento na função que será executada imediatamente .
825
823
function MyPromise(fn) {
826
824
let _this = this;
827
825
_this.currentState = PENDING;
828
826
_this.value = undefined;
829
- // To save the callback of ` then` ,only cached when the state of the promise is pending,
830
- // at most one will be cached in every instance
827
+ // Save o callback do ` then` , apenas em cache quando o estado da promise for pending,
828
+ // no máximo será cacheado em cada instância
831
829
_this.resolvedCallbacks = [];
832
830
_this.rejectedCallbacks = [];
833
831
834
832
_this.resolve = function(value) {
835
- // execute asynchronously to guarantee the execution order
833
+ // execute assícronamente para garantir a ordem de execução
836
834
setTimeout(() => {
837
835
if (value instanceof MyPromise) {
838
- // if value is a Promise, execute recursively
836
+ // se o valor é uma Promise, execute recursivamente
839
837
return value.then(_this.resolve, _this.reject)
840
838
}
841
839
if (_this.currentState === PENDING) {
@@ -847,7 +845,7 @@ function MyPromise(fn) {
847
845
}
848
846
849
847
_this.reject = function(reason) {
850
- // execute asynchronously to guarantee the execution order
848
+ // execute assícronamente para garantir a ordem de execução
851
849
setTimeout(() => {
852
850
if (_this.currentState === PENDING) {
853
851
_this.currentState = REJECTED;
@@ -857,7 +855,7 @@ function MyPromise(fn) {
857
855
})
858
856
}
859
857
860
- // to solve the following problem
858
+ // para resolver o seguinte problema
861
859
// ` new Promise (() => throw Error (' error))`
862
860
try {
863
861
fn(_this.resolve, _this.reject);
@@ -868,10 +866,10 @@ function MyPromise(fn) {
868
866
869
867
MyPromise.prototype.then = function(onResolved, onRejected) {
870
868
const self = this;
871
- // specification 2.2.7, `then` must return a new promise
869
+ // especificação 2.2.7, `then` deve retornar uma nova promise
872
870
let promise2;
873
- // specification 2.2, both `onResolved` and `onRejected` are optional arguments
874
- // it should be ignored if `onResolved` or `onRjected` is not a function ,
871
+ // especificação 2.2, ambos `onResolved` e `onRejected` são argumentos opcionais
872
+ // isso deveria ser ignorado se `onResolved` ou `onRjected` não for uma função ,
875
873
// which implements the penetrate pass of it' s value
876
874
// `Promise.resolve(4).then().then((value) => console.log(value))`
877
875
onResolved = typeof onResolved === ' function' ? onResolved : v => v;
0 commit comments