@todojs/

8.-promise-all

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6200929135472308","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"InVzZSBzdHJpY3QiOw0KDQpmdW5jdGlvbiBwcm9taXNlU3FydCh2YWx1ZSl7DQogICAgcmV0dXJuIG5ldyBQcm9taXNlKGZ1bmN0aW9uIChmdWxmaWxsLCByZWplY3Qpew0KICAgICAgICBjb25zb2xlLmxvZygnU1RBUlQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIHZhbHVlKTsNCiAgICAgICAgc2V0VGltZW91dChmdW5jdGlvbigpIHsNCiAgICAgICAgICAgIGZ1bGZpbGwoeyB2YWx1ZTogdmFsdWUsIHJlc3VsdDogdmFsdWUgKiB2YWx1ZSB9KTsNCiAgICAgICAgfSwgMCB8IE1hdGgucmFuZG9tKCkgKiAxMDApOw0KICAgIH0pOw0KfQ0KDQp2YXIgcCA9IFtdOw0KZm9yICh2YXIgbiA9IDA7IG4gPCAxMDsgbisrKSB7DQogICAgcC5wdXNoKHByb21pc2VTcXJ0KG4sIG4gKiAyKSk7DQp9DQpQcm9taXNlLmFsbChwKS50aGVuKGZ1bmN0aW9uKHJlc3VsdHMpIHsNCiAgICByZXN1bHRzLmZvckVhY2goZnVuY3Rpb24ob2JqKSB7DQogICAgICAgIGNvbnNvbGUubG9nKCdFTkQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIG9iai52YWx1ZSwNCiAgICAgICAgICAgICdhbmQgcmVzdWx0ID0nLCBvYmoucmVzdWx0KTsNCiAgICB9KTsNCiAgICBjb25zb2xlLmxvZygnQ09NUExFTlRFRCcpOw0KfSk7IA=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"use strict";

function promiseSqrt(value){
    return new Promise(function (fulfill, reject){
        console.log('START execution with value =', value);
        setTimeout(function() {
            fulfill({ value: value, result: value * value });
        }, 0 | Math.random() * 100);
    });
}

var p = [];
for (var n = 0; n < 10; n++) {
    p.push(promiseSqrt(n, n * 2));
}
Promise.all(p).then(function(results) {
    results.forEach(function(obj) {
        console.log('END execution with value =', obj.value,
            'and result =', obj.result);
    });
    console.log('COMPLENTED');
}); 
Native Browser JavaScript