Promise 全部源码
时间: 2023-10-21 18:16:44 浏览: 102
完整的源代码
Promise 是 JavaScript 中的一种异步编程解决方案,其源码实现非常复杂,且涉及到很多底层知识,可能不适合初学者直接阅读。以下是 Promise 的部分源码实现,仅供参考:
```javascript
// 定义 Promise 构造函数
function Promise(fn) {
var self = this;
self.value = null;
self.error = null;
self.onFulfilled = [];
self.onRejected = [];
function resolve(value) {
setTimeout(function() {
self.value = value;
self.onFulfilled.forEach(function(callback) {
callback(value);
})
}, 0)
}
function reject(error) {
setTimeout(function() {
self.error = error;
self.onRejected.forEach(function(callback) {
callback(error);
})
}, 0)
}
fn(resolve, reject);
}
// 定义 Promise 原型方法 then
Promise.prototype.then = function(onFulfilled, onRejected) {
var self = this;
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : function(value) { return value };
onRejected = typeof onRejected === 'function' ? onRejected : function(error) { throw error };
var promise2 = new Promise(function(resolve, reject) {
if (self.error) {
setTimeout(function() {
try {
var x = onRejected(self.error);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0)
} else if (self.value) {
setTimeout(function() {
try {
var x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0)
} else {
self.onFulfilled.push(function(value) {
setTimeout(function() {
try {
var x = onFulfilled(value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0)
})
self.onRejected.push(function(error) {
setTimeout(function() {
try {
var x = onRejected(error);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0)
})
}
})
return promise2;
}
// 定义 resolvePromise 函数
function resolvePromise(promise, x, resolve, reject) {
if (promise === x) {
reject(new TypeError('Circular reference detected'));
}
if (x && typeof x === 'object' || typeof x === 'function') {
var then = x.then;
var called = false;
if (typeof then === 'function') {
try {
then.call(x, function(y) {
if (called) return;
called = true;
resolvePromise(promise, y, resolve, reject);
}, function(error) {
if (called) return;
called = true;
reject(error);
})
} catch (error) {
if (called) return;
called = true;
reject(error);
}
} else {
resolve(x);
}
} else {
resolve(x);
}
}
// 定义 Promise 静态方法 all
Promise.all = function(promises) {
return new Promise(function(resolve, reject) {
var result = [];
var count = 0;
promises.forEach(function(promise, index) {
promise.then(function(value) {
result[index] = value;
count++;
if (count === promises.length) {
resolve(result);
}
}, function(error) {
reject(error);
})
})
})
}
// 定义 Promise 静态方法 race
Promise.race = function(promises) {
return new Promise(function(resolve, reject) {
promises.forEach(function(promise) {
promise.then(function(value) {
resolve(value);
}, function(error) {
reject(error);
})
})
})
}
// 定义 Promise 静态方法 resolve
Promise.resolve = function(value) {
return new Promise(function(resolve, reject) {
resolve(value);
})
}
// 定义 Promise 静态方法 reject
Promise.reject = function(error) {
return new Promise(function(resolve, reject) {
reject(error);
})
}
```
以上是 Promise 的部分源码实现,其中包括 Promise 构造函数、then 方法、resolvePromise 函数和 Promise 静态方法 all、race、resolve 和 reject。这些源码实现并不完整,但可以帮助初学者了解 Promise 的基本实现原理。
阅读全文