-
Notifications
You must be signed in to change notification settings - Fork 0
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
Promise的源码实现 #71
Labels
Comments
Open
测试用例 "use strict";
let PromiseCustom = require("./PromiseCustom");
{
const promiseA = new PromiseCustom((resolve, reject) => {
resolve(777);
});
// At this point, "promiseA" is already settled.
promiseA.then((val) => console.log("asynchronous logging has val:", val));
console.log("immediate logging");
// produces output in this order:
// immediate logging
// asynchronous logging has val: 777
}
// ---------------------------------------------------------------------------------------
{
const promise1 = PromiseCustom.resolve(123);
promise1.then((value) => {
console.log(value);
// Expected output: 123
});
// ---------------------------------------------------------------------------------------
function resolved(result) {
console.log('Resolved');
}
function rejected(result) {
console.error(result);
}
Promise.reject(new Error('fail')).then(resolved, rejected);
// Expected output: Error: fail
}
// ---------------------------------------------------------------------------------------
{
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
// Expected output: Array [3, 42, "foo"]
}
// ---------------------------------------------------------------------------------------
{
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'two');
});
Promise.race([promise1, promise2]).then((value) => {
console.log(value);
// Both resolve, but promise2 is faster
});
// Expected output: "two"
}
// ---------------------------------------------------------------------------------------
{
const promise1 = new PromiseCustom((resolve, reject) => {
throw new Error('Uh-oh!');
});
promise1.catch((error) => {
console.error(error);
});
// Expected output: Error: Uh-oh!
}
// ---------------------------------------------------------------------------------------
{
new PromiseCustom()
.finally(() => {
console.log('Experiment completed');
});
}
// ---------------------------------------------------------------------------------------
{
const promise1 = PromiseCustom.resolve(3);
const promise2 = new PromiseCustom((resolve, reject) => setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];
Promise.allSettled(promises).
then((results) => results.forEach((result) => console.log(result.status)));
// Expected output:
// "fulfilled"
// "rejected"
}
// ---------------------------------------------------------------------------------------
{
const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow'));
const promises = [promise1, promise2, promise3];
Promise.any(promises).then((value) => console.log(value));
// Expected output: "quick"
} |
function executePromise(promises) {
if (!Array.isArray(promises)) {
return Promise.reject(new TypeError('Arguments is not iterable'));
}
const result = [];
let len = promises.length;
if (!len) {
return Promise.resolve(result);
}
function executeNextPromise(index) {
if (index >= len) {
return Promise.resolve(result);
}
return promises[index]().then(
(value) => {
result[index] = {
status: 'fulfilled',
reason: value,
};
return executeNextPromise(index + 1);
},
(error) => {
result[i] = {
status: 'rejected',
reason: error,
};
return executeNextPromise(index + 1);
},
);
}
return executeNextPromise(0);
}
// 示例用法
const promise1 = () =>
new Promise((resolve) =>
setTimeout(() => {
console.log(1);
resolve(1);
}, 200),
);
const promise2 = () =>
new Promise((resolve) =>
setTimeout(() => {
console.log(2);
resolve(2);
}, 100),
);
const promise3 = () =>
new Promise((resolve) =>
setTimeout(() => {
console.log(3);
resolve(3);
}, 200),
);
const promises = [promise1, promise2, promise3];
executePromise(promises)
.then((result) => {
console.log('done');
console.log(result); // 输出 [1, 2, 3]
})
.catch((error) => {
console.error(error);
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考文章
The text was updated successfully, but these errors were encountered: