@todojs/

13.-myCo-generadora

JavaScript

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9037958585244319","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"InVzZSBzdHJpY3QiOw0KDQpmdW5jdGlvbiBwcm9taXNlU3FydCh2YWx1ZSl7DQogICAgcmV0dXJuIG5ldyBQcm9taXNlKGZ1bmN0aW9uIChmdWxmaWxsLCByZWplY3Qpew0KICAgICAgICBjb25zb2xlLmxvZygnU1RBUlQgZXhlY3V0aW9uIHdpdGggdmFsdWUgPScsIHZhbHVlKTsNCiAgICAgICAgc2V0VGltZW91dChmdW5jdGlvbigpIHsNCiAgICAgICAgICAgIGZ1bGZpbGwoeyB2YWx1ZTogdmFsdWUsIHJlc3VsdDogdmFsdWUgKiB2YWx1ZSB9KTsNCiAgICAgICAgfSwgMCB8IE1hdGgucmFuZG9tKCkgKiAxMDApOw0KICAgIH0pOw0KfQ0KDQpmdW5jdGlvbiBteUNvKGdlbikgew0KICAgIHZhciBpID0gZ2VuKCk7DQogICAgZnVuY3Rpb24gc2VxdWVudChyZXN1bHQpIHsNCiAgICAgICAgdmFyIHJldCA9IGkubmV4dChyZXN1bHQpOw0KICAgICAgICBpZiAoIXJldC5kb25lKSB7DQogICAgICAgICAgICByZXQudmFsdWUudGhlbihzZXF1ZW50KTsNCiAgICAgICAgfQ0KICAgIH0NCiAgICBzZXF1ZW50KCk7DQp9DQoNCm15Q28oZnVuY3Rpb24qIGdlbigpIHsNCiAgICBmb3IgKHZhciBuID0gMDsgbiA8PSA5OyBuKyspIHsNCiAgICAgICAgdmFyIG9iaiA9IHlpZWxkIHByb21pc2VTcXJ0KG4pOw0KICAgICAgICBjb25zb2xlLmxvZygnRU5EIGV4ZWN1dGlvbiB3aXRoIHZhbHVlID0nLCBvYmoudmFsdWUsICdhbmQgcmVzdWx0ID0nLCBvYmoucmVzdWx0KTsNCiAgICB9DQp9KTsgDQo="},"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
"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);
    });
}

function myCo(gen) {
    var i = gen();
    function sequent(result) {
        var ret = i.next(result);
        if (!ret.done) {
            ret.value.then(sequent);
        }
    }
    sequent();
}

myCo(function* gen() {
    for (var n = 0; n <= 9; n++) {
        var obj = yield promiseSqrt(n);
        console.log('END execution with value =', obj.value, 'and result =', obj.result);
    }
}); 
Native Browser JavaScript