小公司会让手写promise吗
时间: 2023-04-08 14:04:15 浏览: 103
这个问题属于技术问题,我可以回答。小公司可能会让手写promise,因为promise是JavaScript中非常重要的概念,掌握promise可以提高代码的可读性和可维护性。但是,如果公司有足够的预算和人力资源,他们可能会选择使用现成的promise库,比如Bluebird或者Q。
相关问题
手写promise
好的,以下是一个简单的手写 Promise 实现:
```javascript
class MyPromise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = [];
this.onRejectedCallbacks = [];
const resolve = value => {
if (this.state === 'pending') {
this.state = 'fulfilled';
this.value = value;
this.onResolvedCallbacks.forEach(cb => cb());
}
};
const reject = reason => {
if (this.state === 'pending') {
this.state = 'rejected';
this.reason = reason;
this.onRejectedCallbacks.forEach(cb => cb());
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value;
onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason };
const promise2 = new MyPromise((resolve, reject) => {
if (this.state === 'fulfilled') {
setTimeout(() => {
try {
const x = onFulfilled(this.value);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
}
if (this.state === 'rejected') {
setTimeout(() => {
try {
const x = onRejected(this.reason);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
}
if (this.state === 'pending') {
this.onResolvedCallbacks.push(() => {
setTimeout(() => {
try {
const x = onFulfilled(this.value);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try {
const x = onRejected(this.reason);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
}
});
return promise2;
}
catch(onRejected) {
return this.then(null, onRejected);
}
resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected for promise'));
}
let called = false;
if (x instanceof MyPromise) {
x.then(value => {
this.resolvePromise(promise2, value, resolve, reject);
}, reason => {
if (called) return;
called = true;
reject(reason);
});
} else if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
try {
const then = x.then;
if (typeof then === 'function') {
then.call(x, value => {
if (called) return;
called = true;
this.resolvePromise(promise2, value, resolve, reject);
}, reason => {
if (called) return;
called = true;
reject(reason);
});
} else {
resolve(x);
}
} catch (error) {
if (called) return;
called = true;
reject(error);
}
} else {
resolve(x);
}
}
static resolve(value) {
if (value instanceof MyPromise) {
return value;
}
return new MyPromise(resolve => resolve(value));
}
static reject(reason) {
return new MyPromise((resolve, reject) => reject(reason));
}
static all(promises) {
return new MyPromise((resolve, reject) => {
const values = [];
let count = 0;
for (let i = 0; i < promises.length; i++) {
MyPromise.resolve(promises[i]).then(value => {
values[i] = value;
count++;
if (count === promises.length) {
resolve(values);
}
}, reason => {
reject(reason);
});
}
});
}
static race(promises) {
return new MyPromise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
MyPromise.resolve(promises[i]).then(value => {
resolve(value);
}, reason => {
reject(reason);
});
}
});
}
}
```
这个手写 Promise 实现基本上实现了 Promise/A+ 规范,并支持 then、catch、resolve、reject、all、race 等方法。
手写 Promise
当然可以!以下是一个简单的手写Promise的示例:
```javascript
class Promise {
constructor(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onResolveCallbacks = [];
this.onRejectCallbacks = [];
const resolve = (value) => {
if (this.state === 'pending') {
this.state = 'fulfilled';
this.value = value;
this.onResolveCallbacks.forEach(callback => callback());
}
};
const reject = (reason) => {
if (this.state === 'pending') {
this.state = 'rejected';
this.reason = reason;
this.onRejectCallbacks.forEach(callback => callback());
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(onFulfilled, onRejected) {
const fulfilledHandler = typeof onFulfilled === 'function' ? onFulfilled : value => value;
const rejectedHandler = typeof onRejected === 'function' ? onRejected : reason => { throw reason; };
const promise2 = new Promise((resolve, reject) => {
const fulfillHandlerWrapper = () => {
setTimeout(() => {
try {
const result = fulfilledHandler(this.value);
resolvePromise(promise2, result, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
};
const rejectHandlerWrapper = () => {
setTimeout(() => {
try {
const result = rejectedHandler(this.reason);
resolvePromise(promise2, result, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
};
if (this.state === 'fulfilled') {
fulfillHandlerWrapper();
} else if (this.state === 'rejected') {
rejectHandlerWrapper();
} else {
this.onResolveCallbacks.push(fulfillHandlerWrapper);
this.onRejectCallbacks.push(rejectHandlerWrapper);
}
});
return promise2;
}
catch(onRejected) {
return this.then(null, onRejected);
}
}
function resolvePromise(promise, result, resolve, reject) {
if (promise === result) {
return reject(new TypeError('Chaining cycle detected for promise'));
}
let thenCalledOrThrow = false;
if (result instanceof Promise) {
result.then(
value => resolvePromise(promise, value, resolve, reject),
reason => reject(reason)
);
} else if (result !== null && (typeof result === 'object' || typeof result === 'function')) {
try {
const then = result.then;
if (typeof then === 'function') {
then.call(
result,
value => {
if (thenCalledOrThrow) return;
thenCalledOrThrow = true;
resolvePromise(promise, value, resolve, reject);
},
reason => {
if (thenCalledOrThrow) return;
thenCalledOrThrow = true;
reject(reason);
}
);
} else {
resolve(result);
}
} catch (error) {
if (thenCalledOrThrow) return;
thenCalledOrThrow = true;
reject(error);
}
} else {
resolve(result);
}
}
```
这只是一个基本的实现,还有很多细节和功能可以改进。希望对你有所帮助!
阅读全文