Skip to content

promise学习 #18

@yan647

Description

@yan647
  1. 循环+promise

https://www.html.cn/web/javascript/14139.html

https://segmentfault.com/a/1190000014598785

https://cary.im/2019/04/02/ES6%E5%BE%AA%E7%8E%AF%E6%96%B9%E6%B3%95%E7%90%86%E8%A7%A3/

function fn(i) {
  return new Promise((res, rej) => {
    setTimeout(() => {
      console.log('hi', i);
      res();
    }, 1000);
  })
}
​
(async function() {
  for (let i = 0; i < 4; i++) {
    await fn(i);
  }
})()
​
Promise.all([1, 2, 3, 4].map(i => fn(i)))
  .then(results => {
    console.log('results: ', results);
  })
​
Promise.all([1, 2, 3, 4].map(i => fn(i)))
  .then(results => {
    console.log('results: ', results);
  })
​
(async function() {
  [1, 2, 3, 4].map(async i => await fn(i))
})()
​
[1, 2, 3, 4].forEach(async i => await fn(i))
  1. 循环引用

报错 Uncaught (in promise) TypeError: Chaining cycle detected for promise #

https://segmentfault.com/q/1010000022027349/a-1020000022029274

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions