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

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

问题描述

没有找到完整的答案..

Didn't find full answer ..

当 promise 被 yield 时会发生什么?

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)

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

推荐答案

正如 Felix 所说,promise 只是另一个需要产生的值.

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

然而,有一种编写异步代码的风格,它以特定方式使用了 yielded 承诺.这涉及一段调用生成器的周围代码,然后等待生成的承诺解析,然后才向生成器询问下一个值.这允许您将程序编写为:

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);
}

getUserDatacheckUserData 都返回一个 promise.比不写要干净一点

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——你可以谷歌一下.或者你可以自己写:

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 有更多的特性——例如,你可以 yield 一组承诺,它会等待所有的承诺都得到解决,就像 Promise 一样.全部.

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中产生promise时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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