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

It's difference between native and shim #333

Open
david-ni opened this issue May 4, 2018 · 1 comment
Open

It's difference between native and shim #333

david-ni opened this issue May 4, 2018 · 1 comment

Comments

@david-ni
Copy link

david-ni commented May 4, 2018

when I run the following code in browser(chrome v66) that support promise native

let promiseA = new Promise(function(resolve){ resolve('A') }),
    promiseB = new Promise(function(resolve){ resolve(promiseA) }),
    promiseC = new Promise(function(resolve){ resolve('C') });

promiseB.then((arg)=>{ console.log(arg); });
promiseC.then((arg)=>{ console.log(arg); });

the result is C A
but when I run the same code with es6-promise,the result is reverse: A C
which one is right?

@Aqours
Copy link

Aqours commented May 8, 2018

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; otherwise the returned promise will be fulfilled with the value.

Unlike old-style passed-in callbacks, a promise comes with some guarantees:

  • Callbacks will never be called before the completion of the current run of the JavaScript event loop.
  • Callbacks added with .then even after the success or failure of the asynchronous operation, will be called, as above.
  • Multiple callbacks may be added by calling .then several times, to be executed independently in insertion order.
let promiseA = new Promise(function(resolve){ resolve('A') }),
    promiseC = new Promise(function(resolve){ resolve('C') });

promiseA.then(arg => arg).then((arg)=>{ console.log(arg); });
promiseC.then((arg)=>{ console.log(arg); });

According to MDN docs, your code equal to above code. So the result is C A which is correct, and it's a bug of es6-promise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants