Welcome to JS Bin
Load cached copy from
10
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
  <meta charset="utf-8">
5
  <title>JS Bin</title>
6
</head>
7
<body>
8
9
</body>
10
</html>
23
 
1
var guid = 0;
2
function run() {
3
  guid++;
4
  var id = guid;
5
  return new Promise(resolve => {
6
    // resolve in a random amount of time
7
    setTimeout(function () {
8
      console.log(id);
9
      resolve(id);
10
    }, (Math.random() * 1.5 | 0) * 1000);
11
  });
12
}
13
14
var promises = Array.from({ length: 10 }).reduce(function (acc) {
15
  return acc.then(function (res) {
16
    return run().then(function (result) {
17
      res.push(result);
18
      return res;
19
    });
20
  });
21
}, Promise.resolve([]));
22
23
promises.then(console.log);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x