承诺值和 return 承诺 JS 的总和
Sum of promise values and return promise JS
我有一个任务是创建一个函数 summarize1 来接收 promise 和 returns promise,它们的值之和如下所示:
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {console.log(sum);}); // result: 6
通过此测试实施:
describe('summarize1', () => {
it('should return 9 for given promises', async () => {
const promise1 = Promise.resolve(9);
const res = await summarize1(promise1);
assert.strictEqual(res, 9);
});
it('should return 6 for given promises', async () => {
const promise1 = Promise.resolve(2);
const promise2 = Promise.resolve(4);
const res = await summarize1(promise1, promise2);
assert.strictEqual(res, 6);
});
it('should return 11 for given promises', async () => {
const promise1 = Promise.resolve(3);
const promise2 = new Promise(resolve => resolve(9));
const res = await summarize1(promise1, promise2);
assert.strictEqual(res, 12);
});
it('should return 16 for given promises', async () => {
const promise1 = Promise.resolve(10);
const promise2 = new Promise(resolve => resolve(5));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 300)});
const res = await summarize1(promise1, promise2, promise3);
assert.strictEqual(res, 16);
});
});
还有一个名为 summarize2 的 async 函数,它接收 promise 和 returns promise,它们的值之和如上图所示总结1。
通过此测试:
describe('summarize2', () => {
it('should return 9 for given promises', async () => {
const promise1 = Promise.resolve(9);
const res = await summarize2(promise1);
assert.strictEqual(res, 9);
});
it('should return 6 for given promises', async () => {
const promise1 = Promise.resolve(2);
const promise2 = Promise.resolve(4);
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 6);
});
it('should return 11 for given promises', async () => {
const promise1 = Promise.resolve(3);
const promise2 = new Promise(resolve => resolve(9));
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 12);
});
it('should return 16 for given promises', async () => {
const promise1 = Promise.resolve(10);
const promise2 = new Promise(resolve => resolve(5));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 300)});
const res = await summarize2(promise1, promise2, promise3);
assert.strictEqual(res, 16);
});
it('should return 23 for given promises', async () => {
const promise1 = Promise.resolve(11);
const promise2 = new Promise(resolve => resolve(7));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(5)}, 500)});
const res = await summarize2(promise1, promise2, promise3);
assert.strictEqual(res, 23);
});
it('should return 16 for given promises', async () => {
const promise1 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 500)});
const promise2 = new Promise(resolve => {setTimeout(() => {resolve(3)}, 500)});
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(5)}, 500)});
const promise4 = new Promise(resolve => {setTimeout(() => {resolve(7)}, 500)});
const res = await summarize2(promise1, promise2, promise3, promise4);
assert.strictEqual(res, 16);
});
it('should return 42 for given promises', async () => {
const promise1 = Promise.resolve(21);
const promise2 = Promise.resolve(21);
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 42);
});
});
问题是我不明白如何实现这个任务来满足条件,能够创建我们想要的尽可能多的承诺,然后只是从 resolve 中获得它们的值的总和。
我尝试了 4 次,但没有任何效果,我不知道该怎么做,我搜索了每份文档、每份参考文献,但没有找到我需要的东西。 PS:我是 JS 新手
这是我的第一次尝试:
new Promise((resolve) => resolve(value));
function sum(value){
return new Promise((resolve)=>{
resolve(value+value);
});
}
第二次尝试:
async function getFirstPromise(){
return new Promise((resolve)=>resolve(value1));
}
async function getSecondPromise(){
return new Promise((resolve)=>resolve(value2));
}
const sum = new Promise((resolve)=>{
var promise1 = await getFirstPromise();
var promise2 = await getSecondPromise();
resolve(promise1+promise2);
})
return sum.then(function(sum){
console.log(sum);
});
第三次尝试:
const promise1 = new Promise(function (resolve) {
setTimeout(() => {
resolve(value);
}, 1000);
});
const promise2 = new Promise(function (resolve) {
setTimeout(() => {
resolve(value);
}, 1000);
});
const sum = new Promise(function (resolve) {
setTimeout(() => {
resolve(promise1 + promise2);
}, 1000);
});
sum.then(function () {
return sum;
});
最后一个:
let promise1;
let promise2;
let sum = new Promise((resolve)=>{
resolve(sum);
});
sum.then((value)=>{
return value;
})
也许在某个地方我走对了路,但由于缺乏经验,我不知道我应该做什么以及提前expect.Please help.Thanks
您遗漏了一些 JavaScript 可以做到的关键部分
- 使用rest parameters
在函数中获取可变数量的参数
- 等待多个承诺解决,然后使用Promise.all
做一些事情
const summarize1 = (...promises) => {
return Promise.all(promises).then(nums => {
let sum = 0;
for (const num of nums) {
sum += num;
}
return sum;
})
}
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {
console.log(sum);
});
既然你提到你是 JS 的新手,我没有使用任何不必要的东西来解释上面的解决方案。但是,数组上有一个实用函数,称为 reduce
,可以用来代替上面的 for 循环
const summarize1 = (...promises) => {
return Promise.all(promises).then(nums => nums.reduce((sum, ele) => sum + ele));
}
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {
console.log(sum);
});
我有一个任务是创建一个函数 summarize1 来接收 promise 和 returns promise,它们的值之和如下所示:
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {console.log(sum);}); // result: 6
通过此测试实施:
describe('summarize1', () => {
it('should return 9 for given promises', async () => {
const promise1 = Promise.resolve(9);
const res = await summarize1(promise1);
assert.strictEqual(res, 9);
});
it('should return 6 for given promises', async () => {
const promise1 = Promise.resolve(2);
const promise2 = Promise.resolve(4);
const res = await summarize1(promise1, promise2);
assert.strictEqual(res, 6);
});
it('should return 11 for given promises', async () => {
const promise1 = Promise.resolve(3);
const promise2 = new Promise(resolve => resolve(9));
const res = await summarize1(promise1, promise2);
assert.strictEqual(res, 12);
});
it('should return 16 for given promises', async () => {
const promise1 = Promise.resolve(10);
const promise2 = new Promise(resolve => resolve(5));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 300)});
const res = await summarize1(promise1, promise2, promise3);
assert.strictEqual(res, 16);
});
});
还有一个名为 summarize2 的 async 函数,它接收 promise 和 returns promise,它们的值之和如上图所示总结1。 通过此测试:
describe('summarize2', () => {
it('should return 9 for given promises', async () => {
const promise1 = Promise.resolve(9);
const res = await summarize2(promise1);
assert.strictEqual(res, 9);
});
it('should return 6 for given promises', async () => {
const promise1 = Promise.resolve(2);
const promise2 = Promise.resolve(4);
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 6);
});
it('should return 11 for given promises', async () => {
const promise1 = Promise.resolve(3);
const promise2 = new Promise(resolve => resolve(9));
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 12);
});
it('should return 16 for given promises', async () => {
const promise1 = Promise.resolve(10);
const promise2 = new Promise(resolve => resolve(5));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 300)});
const res = await summarize2(promise1, promise2, promise3);
assert.strictEqual(res, 16);
});
it('should return 23 for given promises', async () => {
const promise1 = Promise.resolve(11);
const promise2 = new Promise(resolve => resolve(7));
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(5)}, 500)});
const res = await summarize2(promise1, promise2, promise3);
assert.strictEqual(res, 23);
});
it('should return 16 for given promises', async () => {
const promise1 = new Promise(resolve => {setTimeout(() => {resolve(1)}, 500)});
const promise2 = new Promise(resolve => {setTimeout(() => {resolve(3)}, 500)});
const promise3 = new Promise(resolve => {setTimeout(() => {resolve(5)}, 500)});
const promise4 = new Promise(resolve => {setTimeout(() => {resolve(7)}, 500)});
const res = await summarize2(promise1, promise2, promise3, promise4);
assert.strictEqual(res, 16);
});
it('should return 42 for given promises', async () => {
const promise1 = Promise.resolve(21);
const promise2 = Promise.resolve(21);
const res = await summarize2(promise1, promise2);
assert.strictEqual(res, 42);
});
});
问题是我不明白如何实现这个任务来满足条件,能够创建我们想要的尽可能多的承诺,然后只是从 resolve 中获得它们的值的总和。
我尝试了 4 次,但没有任何效果,我不知道该怎么做,我搜索了每份文档、每份参考文献,但没有找到我需要的东西。 PS:我是 JS 新手
这是我的第一次尝试:
new Promise((resolve) => resolve(value));
function sum(value){
return new Promise((resolve)=>{
resolve(value+value);
});
}
第二次尝试:
async function getFirstPromise(){
return new Promise((resolve)=>resolve(value1));
}
async function getSecondPromise(){
return new Promise((resolve)=>resolve(value2));
}
const sum = new Promise((resolve)=>{
var promise1 = await getFirstPromise();
var promise2 = await getSecondPromise();
resolve(promise1+promise2);
})
return sum.then(function(sum){
console.log(sum);
});
第三次尝试:
const promise1 = new Promise(function (resolve) {
setTimeout(() => {
resolve(value);
}, 1000);
});
const promise2 = new Promise(function (resolve) {
setTimeout(() => {
resolve(value);
}, 1000);
});
const sum = new Promise(function (resolve) {
setTimeout(() => {
resolve(promise1 + promise2);
}, 1000);
});
sum.then(function () {
return sum;
});
最后一个:
let promise1;
let promise2;
let sum = new Promise((resolve)=>{
resolve(sum);
});
sum.then((value)=>{
return value;
})
也许在某个地方我走对了路,但由于缺乏经验,我不知道我应该做什么以及提前expect.Please help.Thanks
您遗漏了一些 JavaScript 可以做到的关键部分
- 使用rest parameters 在函数中获取可变数量的参数
- 等待多个承诺解决,然后使用Promise.all 做一些事情
const summarize1 = (...promises) => {
return Promise.all(promises).then(nums => {
let sum = 0;
for (const num of nums) {
sum += num;
}
return sum;
})
}
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {
console.log(sum);
});
既然你提到你是 JS 的新手,我没有使用任何不必要的东西来解释上面的解决方案。但是,数组上有一个实用函数,称为 reduce
,可以用来代替上面的 for 循环
const summarize1 = (...promises) => {
return Promise.all(promises).then(nums => nums.reduce((sum, ele) => sum + ele));
}
const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {
console.log(sum);
});