Code Monkey home page Code Monkey logo

Comments (21)

forthealllight avatar forthealllight commented on July 30, 2024 7

v4.0 版本中加的 setTimeout 是起什么作用的, v3.0 里没有提及

你好,then方法返回的也是一个promise,也是一个异步的,这里用setTimeout包裹,使得then方法被调用后,不会阻塞后续的同步任务。

from blog.

mallocLNode avatar mallocLNode commented on July 30, 2024 2

第二个案例为什么会先输出一个undefined呢 原生的直接输出1 没有undefined

第2个案列中,为什么会先输出个undefined的

因为switch没有使用break的话,匹配到了会一直往下执行的,作者漏贴了吧。推荐去把源码下下来。
另外还发现在v3 case "pending"后面也有错的

   let temple=onRejected(self.reason);
   reject(temple)

应该改为

   let temple=onRejected(self.reason);
   resolve(temple)

from blog.

cunjieliu avatar cunjieliu commented on July 30, 2024 1

您好,我想问一下为什么要保存this呀, const self = this;
=..=

不做处理的话,会发生隐式绑定,导致this丢失问题

from blog.

cjinhuo avatar cjinhuo commented on July 30, 2024 1

你好,在第二个案例中switch少了个break;
onRejected(self.reason)
});
// 少了个break;所以导致他们说的undefined
case "resolved":

from blog.

pomelovico avatar pomelovico commented on July 30, 2024

请教一个问题,在v4版本中resolvePromise函数需要4个参数,为啥在then方法的实现里只传了一个promise参数呢

from blog.

Yangfan2016 avatar Yangfan2016 commented on July 30, 2024

v4.0 版本中加的 setTimeout 是起什么作用的, v3.0 里没有提及

from blog.

taokexia avatar taokexia commented on July 30, 2024

请教一个问题,在v4版本中resolvePromise函数需要4个参数,为啥在then方法的实现里只传了一个promise参数呢

推荐去看一下作者的源码,这块代码好像写错了。

from blog.

majunchang avatar majunchang commented on July 30, 2024

第二个案例为什么会先输出一个undefined呢 原生的直接输出1 没有undefined

from blog.

boboox avatar boboox commented on July 30, 2024

prmoise === x 的时候应该reject(new TypeError()) 而不是throw吧?

from blog.

Tmac-1 avatar Tmac-1 commented on July 30, 2024

你好,问一下为什么第一个案例无法处理异步的resolve啊

from blog.

VRCuspid avatar VRCuspid commented on July 30, 2024

您好,我想问一下为什么要保存this呀, const self = this;
=..=

from blog.

ningrixin avatar ningrixin commented on July 30, 2024

什么第一个案例无法处理异步的resolve啊

因为执行then的时候还没有resolve,status还是pending所以不会执行onFullfilled

from blog.

cunjieliu avatar cunjieliu commented on July 30, 2024

prmoise === x 的时候应该reject(new TypeError()) 而不是throw吧?

外层有try catch

from blog.

342x avatar 342x commented on July 30, 2024

第2个案列中,为什么会先输出个undefined的

from blog.

Yxiuchao avatar Yxiuchao commented on July 30, 2024

您好!
有typescript实现的then方法吗?

from blog.

JayTam avatar JayTam commented on July 30, 2024

你好,问一下为什么第一个案例无法处理异步的resolve啊

因为 new myPromise() 执行完同步构造函数进行实例化之后,立马又执行了同步的then方法,constructorthen在一个tick中执行了,没有实现异步。

new myPromise((resolve, reject) => {
    setTimeout(()=>{
       // 过了1s执行了resolve,把状态修改成`onFulfilled`,
      //  但是then已经在初始化Promise后立即执行了,then应该在执行了resolve()之后执行
        resolve("end");
    }, 1000);
}).then((data)=>{
    // 这个匿名回调函数不会执行,因为then执行的时候,状态还是pending
    console.log(data);
})

from blog.

h2190697689 avatar h2190697689 commented on July 30, 2024

第二个案例为什么会先输出一个undefined呢 原生的直接输出1 没有undefined

case “pending” 那缺少个break

from blog.

forthealllight avatar forthealllight commented on July 30, 2024

你好,在第二个案例中switch少了个break;
onRejected(self.reason)
});
// 少了个break;所以导致他们说的undefined
case "resolved":

文章写的代码可能有误,直接看源码里面的,那个是正确的额

from blog.

forthealllight avatar forthealllight commented on July 30, 2024

第二个案例为什么会先输出一个undefined呢 原生的直接输出1 没有undefined

case “pending” 那缺少个break

我手写的文章里面可能有误,直接看我给的源码地址,那里面是对的

from blog.

97Yates05 avatar 97Yates05 commented on July 30, 2024

有个问题,执行器传入的reslove应该是能接受promise或者theable对象并且不会直接将值传入的,但是按照作者的实现,执行器里的reslove只是单纯的传值,这个不是promiseA+的标准吗。。

from blog.

dbwcooper avatar dbwcooper commented on July 30, 2024

@forthealllight resolvePromise 函数使用的地方应该少传了三个参数,

 let temple=onFullfilled(self.value);
 resolvePromise(temple)

应该是这样?

 let temple=onFullfilled(self.value);
 resolvePromise(promise2, temple, resolve, reject)

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.