问题描述
承诺和生成器允许您编写异步代码。我不明白为什么这两种机制都是在ECMA脚本6中引入的。什么时候最好使用承诺,何时生成器?
Promises and generators allow you to write asynchronous code. I do not understand why both of these mechanisms are introduced in ECMA script 6. When is it best to use the promises, and when the generators?
推荐答案
这两种技术之间没有任何异议。他们共同共同互相补充。承诺给您能够获得尚不可用的异步操作的结果。它解决了问题。因此,而不是:
There is no opposition between these two techniques. They coexist together complementing each other nicely. Promises give you capability to get result of an asynchronous operation which does not available yet. It solves Pyramid of Doom problem. So instead of:
function ourImportantFunction(callback) {
//... some code 1
task1(function(val1) {
//... some code 2
task2(val1, function(val2) {
//... some code 3
task3(val2, callback);
});
});
}
你可以写:
function ourImportantFunction() {
return Promise.resolve()
.then(function() {
//... some code 1
return task1(val3)
})
.then(function(val2) {
//... some code 2
return task2(val2)
})
.then(function(val2) {
//... some code 3
return task3(val2);
});
}
ourImportantFunction().then(callback);
但即使有承诺,您必须以异步方式编写代码 - 您必须始终将回调传递给函数。编写异步代码比同步更困难。即使代码很大的承诺,变得很难看到算法(好的,这是非常主观的,有人可以争论,但对于大多数程序员,我认为是真的)。所以我们想以同步方式编写异步代码。那就是发电机来帮助我们的地方。所以代替上面的代码可以写:
But even with promises you must write code in asynchronous fashion - you must always pass callbacks to the functions. Writing asynchronous code is much harder then synchronous. Even with promises when the code is huge it becomes difficult to see the algorithm (well, it's very subjective, someone can argue with it. But for majority of programmers I think it's true). So we want to write asynchronous code in synchronous fashion. That's where generators are coming to help us. So instead of code above you can write:
var ourImportantFunction = spawn(function*() {
//... some code 1
var val1 = yield task1();
//... some code 2
var val2 = yield task2(val1);
//... some code 3
var val3 = yield task3(val2);
return val3;
});
ourImportantFunction().then(callback);
尽可能简单 spawn
喜欢:
function spawn(generator) {
return function() {
var iter = generator.apply(this, arguments);
return Promise.resolve().then(function onValue(lastValue){
var result = iter.next(lastValue);
var done = result.done;
var value = result.value;
if (done) return value; // generator done, resolve promise
return Promise.resolve(value).then(onValue, iter.throw.bind(iter)); // repeat
});
};
}
正如你可以看到值
(某些异步函数的结果 task {N}
)必须是一个承诺。你不能用回调来做到这一点。
As you can see value
(result of some asynchronous function task{N}
) must be a promise. You can't do this with callbacks.
仍然要做的是将 spawn
技术实现为语言本身。所以我们用 async
和 yield
替换 spawn
code> await 并且来到:
What remains to do is to implement spawn
technique into language itself. So we are replacing spawn
with async
and yield
with await
and are coming to ES7 async/await:
var ourImportantFunction = async function() {
//... some code 1
var val1 = await task1();
//... some code 2
var val2 = await task2(val1);
//... some code 3
var val3 = await task3(val2);
return val3;
}
我建议您观看可以更好地理解这一点和其他一些即将到来的技术。如果这个人对你来说太快了,放慢播放速度(右下角的设置,或者只是按[ shift
+ ;
I recommend you to watch this video to more understand this and some other coming techniques. If the guy speaks too fast for you, slow down the speed of playing ("settings" in right bottom corner, or just push [shift
+ <
])
什么是最好的:只是回调或承诺,或承诺与发电机 - 这是非常主观的问题。回调是目前最快的解决方案(现在的本地承诺的表现非常糟糕)。发电机的承诺让您有机会以同步方式编写异步代码。但是现在他们比简单的回调要慢得多。
What is the best: just callbacks, or promises, or promises with generators - this is very subjective question. Callbacks is the fastest solution possible at this time (performance of native promises are very bad now). Promises with generators give you opportunity to write asynchronous code in synchronous fashion. But for now they much slower then simple callbacks.
这篇关于新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗旗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!