在javascript中产生承诺会发生什么? [英] What happens when promise is yielded in javascript?

查看:124
本文介绍了在javascript中产生承诺会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未找到完整答案..

承诺产生后会发生什么?

What happens when promise is yielded?

是这样的建设

var p = new Promise()
p.resolve(value)

function * (){
  yield p
}

相当于

function * (){
  yield value
}

更新

如何混合不同风格的异步编程,例如像koa这样的框架?

How to mix different styles of async programming, for example for such framework as koa?

Koa中间件正在使用生成器,但是有很多好的包是基于promise的( sequelize for ex。)

Koa middlewares are working with generators, but there are lots of good packages which are promise-based (sequelize for ex.)

推荐答案

正如菲利克斯所说,承诺只是另一个值得屈服的价值。

As Felix says, a promise is just another value to be yielded.

然而,有一种编写异步代码的方式,它以特定的方式使用产生的promise。这涉及调用生成器的周围代码,然后等待所产生的承诺解析,然后然后才向生成器请求下一个值。这允许您将程序编写为:

However, there is a style of writing asynchronous code which makes use of yielded promises in a particular way. This involves a surrounding piece of code which invokes the generator, then waits for the yielded promise to resolve, and then and only then asks the generator for the next value. This allows you to write your programs as:

function goGenerator *() {
  var userData = yield getUserData();
  yield checkuserData(userData);
}

其中 getUserData checkUserData 返回一个承诺。它比必须写的更清洁

Where both getUserData and checkUserData return a promise. It's a little bit cleaner than having to write

function goPromises() {
  return getUserData() . then(checkUserData);
}

特别是如果涉及更多承诺。这种基于生成器的样式按顺序读取,让人联想到异步函数方法。

especially if there are more promises involved. This generator-based style reads sequentially, and is reminiscent of an async function approach.

async function goAsyncFunction() {
  var userData = await getUserData();
  return await checkUserData(userData);
}

但异步函数尚未得到广泛支持。基于生成器的方法是一种可以与纯ES6一起使用的方法。

But async functions are not widely supported yet. The generator-based approach is an alternative which works with pure ES6.

正如我所提到的,基于生成器的方法需要一段代码围绕它知道什么与所产生的承诺有关 - 正如我所说的那样,是在再次调用发电机之前等待它们解决。这个经典的实现是 co <​​/ code> - 你可以谷歌。或者你可以写自己的:

As I mentioned, the generator-based approach requires a piece of code "surrounding" it that knows what to do with the yielded promises--which, as I said, is to wait for them to resolve before calling the the generator again. The classic implementation of this is co--you can Google that. Or you can write your own:

function spawn(generator) {
  var iterator = generator();

  return new Promise(
    resolve =>
      function iterate(val) {
        var {value, done} = iterator.next(val);
        if (done) { resolve(val); }
        else { Promise.resolve(value).then(iterate); }
      }() 
  );

} 

现在你运行 spawn(goGenerator) ) spawn 本身会返回一个promise,因此您可以挂起更多内容: spawn(goGenerator)。然后(doMoreStuff)

Now you run spawn(goGenerator). spawn itself returns a promise, so you can hang more stuff of it: spawn(goGenerator) . then(doMoreStuff).

这是一个非常简单的实现。 co <​​/ code>还有更多功能 - 例如,你可以收益一系列承诺,它会等待所有他们要解决,la Promise.all

This is a very simple implementation. co has many more features--for instance, you can yield an array of promises, and it will wait for all of them to resolve, a la Promise.all.

这篇关于在javascript中产生承诺会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆