@todojs/

10.-reduce-promise

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3775356890898689","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"InVzZSBzdHJpY3QiOw0KDQpmdW5jdGlvbiBwcm9taXNlU3FydCh2YWx1ZSl7DQogICAgcmV0dXJuIG5ldyBQcm9taXNlKGZ1bmN0aW9uIChmdWxmaWxsLCByZWplY3Qpew0KICAgICAgICBjb25zb2xlLmxvZygnU1RBUlQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIHZhbHVlKTsNCiAgICAgICAgc2V0VGltZW91dChmdW5jdGlvbigpIHsNCiAgICAgICAgICAgIGZ1bGZpbGwoeyB2YWx1ZTogdmFsdWUsIHJlc3VsdDogdmFsdWUgKiB2YWx1ZSB9KTsNCiAgICAgICAgfSwgMCB8IE1hdGgucmFuZG9tKCkgKiAxMDApOw0KICAgIH0pOw0KfQ0KDQp2YXIgcCA9IFswLDEsMiwzLDQsNSw2LDcsOCw5XTsNCnAucmVkdWNlKA0KICAgIGZ1bmN0aW9uIChzZXF1ZW5jZSwgdmFsdWUpIHsNCiAgICAgICAgcmV0dXJuIHNlcXVlbmNlLnRoZW4oZnVuY3Rpb24oKSB7DQogICAgICAgICAgICByZXR1cm4gcHJvbWlzZVNxcnQodmFsdWUpOw0KICAgICAgICB9KS50aGVuKGZ1bmN0aW9uKG9iaikgew0KICAgICAgICAgICAgY29uc29sZS5sb2coJ0VORCBleGVjdXRpb24gd2l0aCB2YWx1ZSA9Jywgb2JqLnZhbHVlLA0KICAgICAgICAgICAgICAgICAgICAgICAgJ2FuZCByZXN1bHQgPScsIG9iai5yZXN1bHQpOw0KICAgICAgICB9KTsNCiAgICB9LA0KICAgIFByb21pc2UucmVzb2x2ZSgpDQopLnRoZW4oZnVuY3Rpb24oKSB7DQogICAgY29uc29sZS5sb2coJ0NPTVBMRVRFRCcpOw0KfSk7IA=="},"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
23
24
25
"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 = [0,1,2,3,4,5,6,7,8,9];
p.reduce(
    function (sequence, value) {
        return sequence.then(function() {
            return promiseSqrt(value);
        }).then(function(obj) {
            console.log('END execution with value =', obj.value,
                        'and result =', obj.result);
        });
    },
    Promise.resolve()
).then(function() {
    console.log('COMPLETED');
}); 
Native Browser JavaScript