问题描述
以下实现了一个控制流包装器 这基本上是 修改: 根据我更新的回复来考虑回报值: 不是真的。对于做同样的事情,这是一种不同的方法。什么 变为 The following implements a control flow wrapper Is this basically what Edit: In light of the responses I updated to take into consideration return values: Not really. It's a different approach for doing sorta the same thing. What becomes 这篇关于在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;
});
});
}
co
enabling asynchronous code to be delineated only by the yield
keyword.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));
}
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));
}
async/await
turn into is more likeasync function foo() {
const bar = await Bar();
bar++;
const baz = await Baz(bar);
return baz;
}
function foo() {
return Bar()
.then(bar => {
bar++;
return Baz(bar)
.then(baz => {
return baz;
});
});
}