本文介绍了在JavaScript中实现协程控制流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下实现了一个控制流包装器 co ,只能通过 yield 关键字来描述异步代码。 / p>

这基本上是 async / await 在ESwhatever引擎盖?



  co(function *(){console.log( '...');产生一个(); console.log('...');产生两个();})函数co(gFn){var g = gFn(); return Promise.resolve().then(go); function go(){var result = g.next(); if(result.done){return; } if(isPromise(result.value)){return result.value.then(go); //承诺阻止直到解决。 } return Promise.resolve(result); function isPromise(o){return o instanceof Promise;} function one(){return new Promise(resolve => setTimeout(()=>(console.log('one'),resolve()),1000 ));} function two(){return new Promise(resolve => setTimeout(()=>(console.log('two'),resolve()),1000));}  



修改:



根据我更新的回复来考虑回报值:



  co(function *(){console.log('...'); const result1 = yield one(); console.log('result1:',result1); const result2 = yield two(); console.log('result2:',result2); const result3 = yield [one(),two()]; console.log('result3:',result3); const result4 = yield {one:one(),two :两个() }; console.log('result4:',result4);})function co(gFn){var g = gFn();返回Promise.resolve()。然后(go); function go(){var result = g.next(... arguments); if(isPromise(result.value)){return result.value.then(go); } if(Array.isArray(result.value)){return Promise.all(result.value).then(go); } if(isObject(result.value)){var o = {}; var promises = Object.keys(result.value).map(k => result.value [k] .then(r => o [k] = r)); return Promise.all(promises).then(()=> o).then(go); } return Promise.resolve(result); function isPromise(o){return o instanceof Promise;} function isObject(val){return val&& (Object === val.constructor);} function one(){return new Promise(resolve => setTimeout(()=>(console.log('one'),resolve('result 1')), 1000));} function two(){return new Promise(resolve => setTimeout(()=>(console.log('two'),resolve('result 2')),1000));}  

解决方案

不是真的。对于做同样的事情,这是一种不同的方法。什么 async / await 转入更像是

  async function foo( ){
const bar = await Bar();
bar ++;
const baz = await Baz(bar);
返回baz;
}

变为

  function foo(){
return Bar()
.then(bar => {
bar ++;
return Baz(bar)
.then(baz => {
return baz;
});
});
}


The following implements a control flow wrapper co enabling asynchronous code to be delineated only by the yield keyword.

Is this basically what async/await does under the hood in ESwhatever?

co(function*() {
    console.log('...');
    yield one();
    console.log('...');
    yield two();
})


function co(gFn) {
    var g = gFn();

    return Promise.resolve()
        .then(go);

    function go() {
        var result = g.next();
        if(result.done) {
            return;
        }
        if(isPromise(result.value)) {
            return result.value.then(go); // Promises block until resolution.
        }
        return Promise.resolve(result);
    }
}

function isPromise(o) {
    return o instanceof Promise;
}

function one() {
    return new Promise(resolve => setTimeout(() => (console.log('one'), resolve()), 1000));
}

function two() {
    return new Promise(resolve => setTimeout(() => (console.log('two'), resolve()), 1000));
}

Edit:

In light of the responses I updated to take into consideration return values:

co(function*() {
    console.log('...');
    const result1 = yield one();
    console.log('result1: ', result1);
    const result2 = yield two();
    console.log('result2: ', result2);
    const result3 = yield[one(), two()];
    console.log('result3: ', result3);
    const result4 = yield{
        one: one(),
        two: two()
    };
    console.log('result4: ', result4);
})

function co(gFn) {
    var g = gFn();

    return Promise.resolve().then(go);

    function go() {
        var result = g.next(...arguments);
        if (isPromise(result.value)) {
            return result.value.then(go);
        }
        if (Array.isArray(result.value)) {
            return Promise.all(result.value).then(go);
        }
        if (isObject(result.value)) {
            var o = {};
            var promises = Object.keys(result.value).map(k=>result.value[k].then(r=>o[k] = r));
            return Promise.all(promises).then(()=>o).then(go);
        }
        return Promise.resolve(result);
    }
}

function isPromise(o) {
    return o instanceof Promise;
}

function isObject(val) {
    return val && (Object === val.constructor);
}

function one() {
    return new Promise(resolve=>setTimeout(()=>(console.log('one'),
    resolve('result 1')), 1000));
}

function two() {
    return new Promise(resolve=>setTimeout(()=>(console.log('two'),
    resolve('result 2')), 1000));
}

解决方案

Not really. It's a different approach for doing sorta the same thing. What async/await turn into is more like

async function foo() {
  const bar = await Bar();
  bar++;
  const baz = await Baz(bar);
  return baz;
}

becomes

function foo() {
  return Bar()
    .then(bar => {
      bar++;
      return Baz(bar)
        .then(baz => {
          return baz;
        });
    });
}

这篇关于在JavaScript中实现协程控制流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:43