@todojs/

9.-promise-hell

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.10281029222374038","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"InVzZSBzdHJpY3QiOw0KDQpmdW5jdGlvbiBwcm9taXNlU3FydCh2YWx1ZSl7DQogICAgcmV0dXJuIG5ldyBQcm9taXNlKGZ1bmN0aW9uIChmdWxmaWxsLCByZWplY3Qpew0KICAgICAgICBjb25zb2xlLmxvZygnU1RBUlQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIHZhbHVlKTsNCiAgICAgICAgc2V0VGltZW91dChmdW5jdGlvbigpIHsNCiAgICAgICAgICAgIGZ1bGZpbGwoeyB2YWx1ZTogdmFsdWUsIHJlc3VsdDogdmFsdWUgKiB2YWx1ZSB9KTsNCiAgICAgICAgfSwgMCB8IE1hdGgucmFuZG9tKCkgKiAxMDApOw0KICAgIH0pOw0KfQ0KDQpwcm9taXNlU3FydCgwKS50aGVuKGZ1bmN0aW9uKG9iaikgew0KICAgIGNvbnNvbGUubG9nKCdFTkQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIG9iai52YWx1ZSwgJ2FuZCByZXN1bHQgPScsIG9iai5yZXN1bHQpOw0KICAgIHJldHVybiBwcm9taXNlU3FydCgxKTsNCn0pLnRoZW4oZnVuY3Rpb24ob2JqKSB7DQogICAgY29uc29sZS5sb2coJ0VORCBleGVjdXRpb24gd2l0aCB2YWx1ZSA9Jywgb2JqLnZhbHVlLCAnYW5kIHJlc3VsdCA9Jywgb2JqLnJlc3VsdCk7DQogICAgcmV0dXJuIHByb21pc2VTcXJ0KDIpOw0KfSkudGhlbihmdW5jdGlvbihvYmopIHsNCiAgICBjb25zb2xlLmxvZygnRU5EIGV4ZWN1dGlvbiB3aXRoIHZhbHVlID0nLCBvYmoudmFsdWUsICdhbmQgcmVzdWx0ID0nLCBvYmoucmVzdWx0KTsNCiAgICByZXR1cm4gcHJvbWlz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TsNCiAgICByZXR1cm4gcHJvbWlzZVNxcnQoOSk7DQp9KS50aGVuKGZ1bmN0aW9uKG9iaikgew0KICAgIGNvbnNvbGUubG9nKCdFTkQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIG9iai52YWx1ZSwgJ2FuZCByZXN1bHQgPScsIG9iai5yZXN1bHQpOw0KfSkuY2F0Y2goZnVuY3Rpb24oZXJyKSB7DQogICAgY29uc29sZS5lcnJvcihlcnIpOw0KfSk7IA=="},"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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"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);
    });
}

promiseSqrt(0).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(1);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(2);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(3);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(4);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(5);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(6);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(7);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(8);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
    return promiseSqrt(9);
}).then(function(obj) {
    console.log('END execution with value =', obj.value, 'and result =', obj.result);
}).catch(function(err) {
    console.error(err);
}); 
Native Browser JavaScript