Home > Net >  Sum of promise values and return promise JS
Sum of promise values and return promise JS

Time:11-12

I have a task to create a function summarize1 that receives promises and returns promise with sum of their values as shown below:

const promise1 = Promise.resolve(4);
const promise2 = new Promise((resolve) => resolve(2));
summarize1(promise1, promise2).then((sum) => {console.log(sum);}); // result: 6

With this test implementation:

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);
    });
});

And an async function called summarize2 that receives promises and returns promise with sum of their values as shown above in summarize1. With this test:

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);
    });
});

The problem is that i dont understand how to implement this task to satisfy the condition,to be able to create as many promises we want and then just to get the sum of their values from resolve.

I had 4 tries,and nothing works,i dont know what to do anymore,i searched every documentation,every refference,and nothing about what i need. PS: im new to JS

This is my first try:

    new Promise((resolve) => resolve(value));
    function sum(value){
     return new Promise((resolve)=>{
    resolve(value value);
    });
    }

Second try:

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);
    }); 

Third try:

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;
    });
    

And last:

   let promise1;
    let promise2;
    let sum = new Promise((resolve)=>{
resolve(sum);
    });
    sum.then((value)=>{
        return value;
    }) 

Maybe somewhere i go on the right path,but because of the lack of experience,i dont know what i should do and what to expect.Please help.Thanks in advance.

CodePudding user response:

You're missing a few key parts that JavaScript can do

  1. Take variable number of arguments in a function using rest parameters
  2. Wait for multiple promises to resolve and then do something, using 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);
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Since you've mentioned you're new to JS, I didn't use anything more than necessary to explain the solution above. But, there's a utility function on arrays, called reduce, that can be used instead of the for loop above

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);
});
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related