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

6. Promise.resolve #6

Open
Sunny-117 opened this issue Nov 3, 2022 · 6 comments
Open

6. Promise.resolve #6

Sunny-117 opened this issue Nov 3, 2022 · 6 comments

Comments

@Sunny-117
Copy link
Owner

Promise.resolve = function (data) {
    if (data instanceof MyPromise) {
      return data;
    }
    return new MyPromise((resolve, reject) => {
      if (isPromise(data)) {
        data.then(resolve, reject);
      } else {
        resolve(data);
      }
    });
  }
@faga295
Copy link

faga295 commented Nov 12, 2022

Promise.resolve = function (data) {
  if (data instanceof Promise) {
    return data;
  }
  return new Promise((resolve) => {
    resolve(data);
  });
};

instanceof MyPromise和下面的ispromise是不是重复了

@gzaii1
Copy link

gzaii1 commented Nov 14, 2022

@faga295 不重复, 不过确实没补充完全. 这里isPromise应该是要去判断是否thenable.

贴一段V8的源码

if (!IsJSPromiseMap(resolutionMap)) {
      dcheck(IsJSReceiverMap(resolutionMap));
      dcheck(!IsPromiseThenProtectorCellInvalid());
     // 删掉了部分代码....
      return FulfillPromise(promise, resolution);
 }

可见v8在实现resolve时, 把promise和thenable的对象都滤去了, IsJSPromiseMap和IsPromiseThenProtectorCellInvalid这两个函数就是保证最终的value中没有then方法.

事实上你可以试一下:

Promise.resolve({ then: (resolve) => resolve('xx') })
  .then(res => console.log(res))

这里打印出来的并不是一个带有then属性的对象, 而是'xx'

@MrZhang3512
Copy link

const myPromiseReslove = (data) => {
  if (data && data instanceof Promise) {
    return data;
  } 
  return new Promise((resolve, reject) => {
    if (data && data.then && typeof data.then === 'function') {
      data.then(resolve, reject)
    } else {
      resolve(data)
    }
  }) 
  
}

@kangkang123269
Copy link

kangkang123269 commented Feb 20, 2023

function myResolve(value) {
  // 如果 value 已经是 Promise 对象,则直接返回该 Promise 对象
  if (value instanceof Promise) {
    return value;
  }
  // 如果 value 是 thenable 对象,则包装成 Promise 对象并返回
  if (value && typeof value.then === 'function') {
    return new Promise(function(resolve, reject) {
      value.then(resolve, reject);
    });
  }
  // 将传入的值作为 Promise 的成功值,并返回 Promise 对象
  return new Promise(function(resolve) {
    resolve(value);
  });
}

@cscty
Copy link

cscty commented Jun 18, 2023

Promise.resolve = function (data) {
if (data instanceof Promise) return data;
return new Promise((resolve, reject) => {
resolve(data);
});
};

@HuangDaohong
Copy link

Promise.resolve = function (data) {
  if (data instanceof Promise) {
    return data;
  }
  return new Promise((resolve) => {
    resolve(data);
  });
};

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

7 participants