链接异步方法调用 - javascript [英] chaining async method calls - javascript

查看:115
本文介绍了链接异步方法调用 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一个原型对象Foo有两个异步方法调用,bar和baz。

You have a prototype object Foo with two async method calls, bar and baz.

var bob = new Foo()

Foo.prototype.bar = function land(callback) {
  setTimeout(function() {
    callback()
    console.log('bar');
  }, 3000);
};

Foo.prototype.baz = function land(callback) {
  setTimeout(function() {
    callback()
    console.log('baz');
  }, 3000);
};

我们想做bob.bar()。baz()并让它记录bar和baz顺序。

We want to do bob.bar().baz() and have it log "bar" and "baz" sequentially.

如果你不能修改方法调用(包括传入你的回调函数),你如何将默认回调传递给这些方法调用?

If you cannot modify the method calls (including passing in your callback function), how can you pass a default callback into these method calls?

一些想法:


  1. 用装饰器包裹bob(仍然模糊如何实现,可以使用一个小例子)

  1. Wrap "bob" with decorator (still fuzzy on how to implement, could use a small example)

修改构造函数以指定默认回调(如果没有分配)(没有考虑是否可能) )

Modify constructor to assign default callback if none assigned (have not considered if this is possible or not)

使用生成器包装器继续调用next方法,直到没有剩下?

Use a generator wrapper that will continue to call next method until none are left?


推荐答案

更推荐的方法是使用 promises 。因为这是社区范围内做异步事件的趋势。

The more recommended way instead is to use promises. As this is a community-wide trend to do async stuffs.


我们想做bob.bar()。baz()并且有它按顺序记录bar和baz

We want to do bob.bar().baz() and have it log "bar" and "baz" sequentially.

为什么你要这样做只是为了实现这个目的 bob.bar()。baz()语法?如果你能做到这一点,那就简单地使用Promise API而不需要额外的努力来使语法工作确实增加了代码复杂性,使得实际代码难以理解。

Why would you want to do that just to achieve this bob.bar().baz() "syntax"? When you could do it pretty simply using the Promise API without additional efforts to make that syntax work that indeed increases code complexity making the actual code difficult to understand.

所以,你可能想要考虑使用基于承诺的方法,这种方法确实提供了比您的方法更大的灵活性:

So, you might want to consider using the promise-based approach somewhat like this, which does offer much more flexibility than what you would have achieved by your approach:

Foo.prototype.bar = function () {
    return new Promise(function (resolve) {
        setTimeout(function () {
            resolve()
            console.log('bar');
        }, 3000);
    };
};

Foo.prototype.baz = function () {
    return new Promise(function (resolve) {
        setTimeout(function () {
            resolve()
            console.log('baz');
        }, 3000);
    };
};

现在你要这样做来运行它们顺序一个anot之后她:

Now you'd do this to run them sequentially one after another:

var bob = new Foo();

bob.bar().then(function() {
   return bob.baz();
});

// If you're using ES2015+ you could even do:
bob.bar().then(() => bob.baz());

如果您需要连接更多功能,您可以简单地执行此操作:

If you need to chain more functions you could simply do it:

bob.bar()
    .then(() => bob.baz())
    .then(() => bob.anotherBaz())
    .then(() => bob.somethingElse());  

无论如何,如果你不习惯使用承诺,你可能想要读取此内容

Anyway, if you're not used to using promises you might want to read this

这篇关于链接异步方法调用 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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