[Ali front-end evaluation question] Implement the mergePromise function, execute the passed array in sequence, and put the returned data in the array (data)

const timeout = ms => new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, ms)
  });

  const ajax1 = () => timeout(1000).then(() => {
    console.log('1')
    return 1;
  });

  const ajax2 = () => timeout(1000).then(() => {
    console.log('2')
    return 2
  });

  const ajax3 = () => timeout(1000).then(() => {
    console.log('3')
    return 3
  });

  const mergePromise = ajaxArray => {
    let result = []
    let promise = Promise.resolve()
    ajaxArray.forEach(function (item) {
      promise = promise.then(item)
      result.push(promise)
    })
    return Promise.all(result)
  };

  mergePromise([ajax1, ajax2, ajax3]).then(data => {
    console.log('done')
    console.log(data) //[1,2,3]
  });

 

Guess you like

Origin blog.csdn.net/weixin_37719279/article/details/81123133