Skip to content

Instantly share code, notes, and snippets.

This for loop:

for (let i = 0, getI = () => i; i < 3; i++)
  console.log(getI());

unrolls to:

@lovetingyuan
lovetingyuan / promise_polyfill.js
Last active November 17, 2020 02:25
promise polyfill
function resolveValue (promise, value, resolve, reject) {
if (promise === value) { // 不能resolve自身
return reject(new TypeError('Can not resolve or return the current promise.'))
}
if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
return resolve(value)
}
let then // thenable可能是对象或者函数,它的then只能读取一次并且需要捕获可能的错误
try {
then = value.then