Skip to content
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

Day127:按要求完成 mergePromise 代码 #940

Open
Genzhen opened this issue Aug 27, 2020 · 3 comments
Open

Day127:按要求完成 mergePromise 代码 #940

Genzhen opened this issue Aug 27, 2020 · 3 comments
Labels
JavaScript teach_tag 编程题 teach_tag 阿里 company

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Aug 27, 2020

const timeout = (ms) =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, ms);
  });
const ajax1 = () =>
  timeout(2000).then(() => {
    console.log("1");
    return 1;
  });
const ajax2 = () =>
  timeout(1000).then(() => {
    console.log("2");
    return 2;
  });
const ajax3 = () =>
  timeout(2000).then(() => {
    console.log("3");
    return 3;
  });
const mergePromise = (ajaxArray) => {
  // 1,2,3 done [1,2,3] 此处写代码 请写出ES6、ES3 2中解法
};
mergePromise([ajax1, ajax2, ajax3]).then((data) => {
  console.log("done");
  console.log(data); // data 为[1,2,3]
});
// 执行结果为:1 2 3 done [1,2,3]
@Genzhen Genzhen added JavaScript teach_tag 编程题 teach_tag 阿里 company labels Aug 27, 2020
@Genzhen
Copy link
Collaborator Author

Genzhen commented Aug 27, 2020

每日一题会在下午四点在交流群集中讨论,五点 Github、交流群同步更新答案

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。

@luuman
Copy link

luuman commented Nov 1, 2021

const timeout = (ms) => new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve()
  }, ms)
})
const ajax1 = () => timeout(2000).then(() => {
  console.log('1')
  return 1
})
const ajax2 = () => timeout(1000).then(() => {
  console.log('2')
  return 2
})
const ajax3 = () => timeout(2000).then(() => {
  console.log('3')
  return 3
})
const mergePromise = (ajaxArray) => {
	var data = []
	var sequence = Promise.resolve()
	ajaxArray.forEach((item) => {
      console.log('item', item)
		sequence = sequence.then(item).then((res) => {
			data.push(res)
			return data
		})
	})
	return sequence
}
mergePromise([ajax1, ajax2, ajax3]).then((data) => {
  console.log('done')
  // data 为[1,2,3]
  console.log(data)
})
// 执行结果为:1 2 3 done [1,2,3]

@DaphnisLi
Copy link

const timeout = (ms) => new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve()
  }, ms)
})
const ajax1 = () => timeout(2000).then(() => {
  console.log(1)
  return 1
})
const ajax2 = () => timeout(1000).then(() => {
  console.log(2)
  return 2
})
const ajax3 = () => timeout(2000).then(() => {
  console.log(3)
  return 3
})

const mergePromise = (ajaxArray) => {
  return new Promise((resolve, reject) => {
    if (!ajaxArray.length) return resolve([])
    const result = []
    const request = async () => {
      const ajax = ajaxArray.shift()
      const res = await ajax()
      result.push(res)
      if (!ajaxArray.length) {
        resolve(result)
      } else {
        request()
      }
    }
    request()
  })
}
mergePromise([ajax1, ajax2, ajax3]).then((data) => {
  console.log('done')
  console.log(data) // data 为[1,2,3]
})
// 执行结果为:1 2 3 done [1,2,3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 编程题 teach_tag 阿里 company
Projects
None yet
Development

No branches or pull requests

3 participants