
// function* likeFor(end, step = 1, start = 0) {
//     for(let i = start; i < end; i+=step) {
//         yield i;
//         console.log(i)
//     }
// }

// let iter = likeFor(10)

// function* multCall() {
//    yield* likeFor(10)
//    yield* likeFor(20)
//    yield* 'СТОРКА'
// }
// let a = [...multCall()]
// console.log(a)





const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))

function* likeAsync(){
    console.log(1)
    let res1 = yield delay(2000)
    console.log(2, res1)
    let res2 = yield delay(2000)
    console.log(3, res2)
}

function run(likeAsync) {
    let iter = likeAsync()
    function next(result, err) { 
        let value, done
        if (!err) {
            [{value, done}] = [iter.next(result)]
        } else {
            [{value, done}] = [iter.throw(err)]
        }
        if (done) return;
        if(value && typeof value?.then === 'function') {
            value.then(next, err => next(undefined, err))
        } else {
            next(value)
        }
    }
    next()
}
run(likeAsync)